2017-01-16 5 views
13

this questionに基づいて、様々なプロトコルメッセージを異なる方法で処理するために、ネゴシエートされたサブプロトコルに基づいてサーバーエンドポイントインスタンスを作成したいと思います。残念ながらServerEndpointConfig.Configurator.getEndpointInstance [docs]は、関連するセッションデータにアクセスして、ネゴシエートされたサブプロトールを取得することはできないため、別のクラスをインスタンス化できます。Websocket ServerEndpoint instance by subprotocol

public static class ServerEndpointConfigurator extends 
     ServerEndpointConfig.Configurator { 

    public ServerEndpointConfigurator() 
    { 
    } 

    @Override 
    public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) { 
     // useful to work with session data in endpoint instance but not at getEndpointInstance 
     HttpSession httpSession = (HttpSession) request.getHttpSession(); 
     config.getUserProperties().put(HttpSession.class.getName(), httpSession); 
    } 

    @Override 
    public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException { 

     // TODO get negotiated subprotocol and instantiate endpoint using switch case or factory 

     return (T) new WebSocketControllerA(); 

     // or return (T) new WebSocketControllerB(); 
     // or return (T) new WebSocketControllerC(); 
     // ... 
    } 
} 

どのようにこの問題を解決するか、あるいは広く受け入れられているプラ​​クティスはどのように異なるサブプロトコルを扱うのですか?私は、Web上のサブプロトコル処理に関するサンプル実装や高度なドキュメントを見つけるのに苦労しています。

答えて

0

これはあなたが探しているものですか?

@ServerEndpoint("/ws") 
public class MyWebSocket { 

    @OnOpen 
    public void onOpen(Session session) { 
     session.getNegotiatedSubprotocol(); 
    } 
関連する問題