Java EE 7當中的編程式Websocket端點
這篇文章將簡要示範如何利用Java Websocket API的編程式版本開發並部署伺服器與用戶端)websocket端點。
第一步——擴充javax.websocket.Endpoint類
- 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());
-
- }
-
- }
接下來我們還要編寫用戶端端點使用同樣的API組合):
- 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...
-
- }
第二步——實現ServerApplicationConfig介面
該介面屬於javax.websocket.server包的組成部分,且能夠通過覆寫以實現端點部署中的定製化邏輯適用於注釋以及編程式端點)。
- 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();
-
- }
-
- }
怎樣處理Client端點?
如果有必要,大家也可以建立自己的ClientEndpointConfig執行個體,並利用它對指向websocket伺服器端點的串連進行初始化。
- 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"));
注意事項:
- 用戶端以及伺服器端的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】