通过子协议Websocket ServerEndpoint实例

时间:2017-01-16 14:24:45

标签: java java-ee websocket javax

基于this question我想基于协商的子协议创建服务器端点实例,以不同方式处理各种协议消息。不幸的是ServerEndpointConfig.Configurator.getEndpointInstance [docs]不允许我访问任何相关的会话数据来获取协商的子协议,因此我可以实例化不同的类。

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 个答案:

没有答案