Java EE 7當中的編程式Websocket端點

來源:互聯網
上載者:User

Java EE 7當中的編程式Websocket端點

這篇文章將簡要示範如何利用Java Websocket API的編程式版本開發並部署伺服器與用戶端)websocket端點。

第一步——擴充javax.websocket.Endpoint類

  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.  
  45. } 

接下來我們還要編寫用戶端端點使用同樣的API組合):

  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.  
  13. } 

第二步——實現ServerApplicationConfig介面

該介面屬於javax.websocket.server包的組成部分,且能夠通過覆寫以實現端點部署中的定製化邏輯適用於注釋以及編程式端點)。

  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.  
  35. } 

怎樣處理Client端點?

如果有必要,大家也可以建立自己的ClientEndpointConfig執行個體,並利用它對指向websocket伺服器端點的串連進行初始化。

  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")); 

注意事項:

  • 用戶端以及伺服器端的config對象只能為等同於編程式)對象的@ServerEndpoint以及@ClientEndpoint注釋元素例如值、編碼器、解碼器以及配置器等等)。
  • 各獨立builder類ServerEndpointConfig.Builder與ClientEndpointConfig.Builder)用於分別建立伺服器與用戶端配置執行個體。
  • ServerEndpointConfig執行個體的建立屬於mandatory,因為伺服器端點無法在不配合URI的前提下進行部署。不過這種情況在用戶端端點方面不會出現——因為其全部作用就是接入現有伺服器端點。
  • 端點config伺服器&用戶端)擁有configurator概念,其可通過對應builder方法進行建立與設定。
  • 接下來我們還將發布更多與Websocket相關的指導性文章,各位敬請期待。

原文標題:Programmatic Websocket Endpoints in Java EE 7

Bkjia譯稿,合作網站轉載請註明原文譯者和出處為bkjia.com】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.