Ben*_*Ben 15 java java-ee websocket javax
基于这个问题,我想基于协商的子协议创建一个服务器端点实例,以不同的方式处理各种协议消息.不幸的是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();
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
有任何想法如何解决这个问题或有任何广泛接受的做法如何处理不同的子协议?我很难找到关于网络上的子协议处理的示例实现或高级文档.
这是你想要的?
@ServerEndpoint("/ws")
public class MyWebSocket {
@OnOpen
public void onOpen(Session session) {
session.getNegotiatedSubprotocol();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
562 次 |
| 最近记录: |