13
根據this question我想創建一個基於協商的子協議的服務器端點實例來處理不同的協議消息。不幸的是ServerEndpointConfig.Configurator.getEndpointInstance
[docs]不會讓我訪問任何相關的會話數據來獲得協商的子原型,所以我可以實例化不同的類。Websocket ServerEndpoint實例按子協議
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上的子協議處理的示例實現或高級文檔。