Programming Websocket endpoint in Java EE 7
This article will briefly demonstrate how to develop and deploy the Websocket endpoint by using the programming version of Java websocket API.
Step 1 -- extend the javax. websocket. Endpoint class
- public class ProgrammaticEchoEnpoint extends Endpoint {
-
- @Override
-
- public void onOpen(Session session, EndpointConfig config) {
-
- System.out.println("Peer " + session.getId() + " connected");
-
- session.addMessageHandler(new MessageHandler.Whole<String>() {
-
- @Override
-
- public void onMessage(String message) {
-
- try {
-
- session.getBasicRemote().sendText("Got message from " + session.getId() + "\n" + message);
-
- } catch (IOException ex) {
-
- }
-
- }
-
- });
-
- }
-
- @Override
-
- public void onClose(Session session, CloseReason closeReason) {
-
- System.out.println("Peer " + session.getId() + " disconnected due to " + closeReason.getReasonPhrase());
-
- }
-
- @Override
-
- public void onError(Session session, Throwable error) {
-
- System.out.println("Error communicating with peer " + session.getId() + ". Detail: "+ error.getMessage());
-
- }
-
- }
Next we need to write the client endpoint to use the same API combination ):
- public class ProgrammaticEchoClient extends Endpoint {
-
- @Override
-
- public void onOpen(Session session, EndpointConfig config) {
-
- System.out.println("Connected to server");
-
- }
-
- //a message handler and other life cycle implementations have been skipped on purpose...
-
- }
Step 2 -- Implement the ServerApplicationConfig Interface
This interface is part of the javax. websocket. server package, and can be overwritten to implement customized logic in the endpoint deployment for annotation and programming endpoint ).
- public class CustomServerAppConfigProvider implements ServerApplicationConfig {
-
- @Override
-
- public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> endpointClasses) {
-
- Set<ServerEndpointConfig> result = new HashSet<>();
-
- for (Class epClass : endpointClasses) {
-
- //need to ignore Client endpoint class
-
- if (epClass.equals(ProgrammaticChatEndpoint.class)) {
-
- ServerEndpointConfig sec = ServerEndpointConfig.Builder.create(epClass, "/letschat").build();
-
- result.add(sec);
-
- }
-
- }
-
- return result;
-
- }
-
- @Override
-
- public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scanned) {
-
- return Collections.emptySet();
-
- }
-
- }
How to handle the Client endpoint?
If necessary, you can also create your own ClientEndpointConfig instance and use it to initialize the connection to the websocket server endpoint.
- WebSocketContainer webSocketContainer = ContainerProvider.getWebSocketContainer();
-
- ClientEndpointConfig config = ClientEndpointConfig.Builder.create().decoders(StockTickDecoder.class).build();
-
- Session session = webSocketContainer.connectToServer(StockTickerClient().class, config,
-
- new URI("ws://hotstocks.com/ticker"));
Note:
- The config object on the client and server can only be equivalent to the @ ServerEndpoint and @ ClientEndpoint annotation elements such as values, encoders, decoders, and configurators of the program ).
- ServerEndpointConfig. builder and ClientEndpointConfig. Builder of each independent Builder class are used to create server and client configuration instances respectively.
- The creation of the ServerEndpointConfig instance belongs to mandatory, because the server endpoint cannot be deployed without URI. However, this situation does not occur on the client endpoint-because it is used to access the existing Server endpoint.
- The config server and client have the aggregator concept, which can be created and set through the corresponding builder method.
- Next, we will release more guidance articles related to Websocket.
Original article title: Programmatic Websocket Endpoints in Java EE 7
Bkjia translation. if the site is reprinted, indicate the original translator and the source is bkjia.com]