Programming Websocket endpoint in Java EE 7

Source: Internet
Author: User

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

 
 
  1. public class ProgrammaticEchoEnpoint extends Endpoint { 
  2.  
  3.     @Override 
  4.  
  5.     public void onOpen(Session session, EndpointConfig config) { 
  6.  
  7.         System.out.println("Peer " + session.getId() + " connected"); 
  8.  
  9.         session.addMessageHandler(new MessageHandler.Whole<String>() { 
  10.  
  11.             @Override 
  12.  
  13.             public void onMessage(String message) { 
  14.  
  15.                 try { 
  16.  
  17.                     session.getBasicRemote().sendText("Got message from " + session.getId() + "\n" + message); 
  18.  
  19.                 } catch (IOException ex) { 
  20.  
  21.                 } 
  22.  
  23.             } 
  24.  
  25.         }); 
  26.  
  27.     } 
  28.  
  29.     @Override 
  30.  
  31.     public void onClose(Session session, CloseReason closeReason) { 
  32.  
  33.         System.out.println("Peer " + session.getId() + " disconnected due to " + closeReason.getReasonPhrase()); 
  34.  
  35.     } 
  36.  
  37.     @Override 
  38.  
  39.     public void onError(Session session, Throwable error) { 
  40.  
  41.         System.out.println("Error communicating with peer " + session.getId() + ". Detail: "+ error.getMessage()); 
  42.  
  43.     } 
  44.  

Next we need to write the client endpoint to use the same API combination ):

 
 
  1. public class ProgrammaticEchoClient extends Endpoint { 
  2.  
  3.     @Override 
  4.  
  5.     public void onOpen(Session session, EndpointConfig config) { 
  6.  
  7.         System.out.println("Connected to server"); 
  8.  
  9.     } 
  10.  
  11.     //a message handler and other life cycle implementations have been skipped on purpose... 
  12.  

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 ).

 
 
  1. public class CustomServerAppConfigProvider implements ServerApplicationConfig { 
  2.  
  3.     @Override 
  4.  
  5.     public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> endpointClasses) { 
  6.  
  7.         Set<ServerEndpointConfig> result = new HashSet<>(); 
  8.  
  9.         for (Class epClass : endpointClasses) { 
  10.  
  11.             //need to ignore Client endpoint class 
  12.  
  13.             if (epClass.equals(ProgrammaticChatEndpoint.class)) { 
  14.  
  15.                 ServerEndpointConfig sec = ServerEndpointConfig.Builder.create(epClass, "/letschat").build(); 
  16.  
  17.                 result.add(sec); 
  18.  
  19.             } 
  20.  
  21.         } 
  22.  
  23.         return result; 
  24.  
  25.     } 
  26.  
  27.     @Override 
  28.  
  29.     public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scanned) { 
  30.  
  31.         return Collections.emptySet(); 
  32.  
  33.     } 
  34.  

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.

 
 
  1. WebSocketContainer webSocketContainer = ContainerProvider.getWebSocketContainer(); 
  2.  
  3. ClientEndpointConfig config = ClientEndpointConfig.Builder.create().decoders(StockTickDecoder.class).build(); 
  4.  
  5. Session session = webSocketContainer.connectToServer(StockTickerClient().class, config,  
  6.  
  7.                                                     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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.