2017-01-16 8 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(); 
     // ... 
    } 
} 

이 문제를 해결하는 방법이나 다른 하위 프로토콜을 처리하는 방법이 널리 수용되는 방법이 있습니까? 웹에서 하위 프로토콜 처리에 대한 예제 구현 또는 고급 설명서를 찾는 데 어려움을 겪고 있습니다.

답변

0

여기를 클릭하십시오.

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

    @OnOpen 
    public void onOpen(Session session) { 
     session.getNegotiatedSubprotocol(); 
    }