Java 按子目录列出的Websocket ServerEndpoint实例
基于,我想基于协商的子脚本创建一个服务器端点实例,以不同方式处理各种协议消息。不幸的是,Java 按子目录列出的Websocket ServerEndpoint实例,java,jakarta-ee,websocket,javax,Java,Jakarta Ee,Websocket,Javax,基于,我想基于协商的子脚本创建一个服务器端点实例,以不同方式处理各种协议消息。不幸的是,ServerEndpointConfig.Configurator.getEndpointInstance[]不允许我访问任何相关的会话数据来获取协商的子线程,因此我可以实例化不同的类 public static class ServerEndpointConfigurator extends ServerEndpointConfig.Configurator { public Se
ServerEndpointConfig.Configurator.getEndpointInstance
[]不允许我访问任何相关的会话数据来获取协商的子线程,因此我可以实例化不同的类
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();
// ...
}
}
公共静态类ServerEndpointConfigurator扩展
ServerEndpointConfig.Configurator{
公共ServerEndpointConfigurator()
{
}
@凌驾
public void modifyHandshake(ServerEndpointConfig配置、HandshakeRequest请求、HandshakeResponse响应){
//在端点实例中使用会话数据很有用,但在getEndpointInstance中不使用
HttpSession HttpSession=(HttpSession)请求。getHttpSession();
config.getUserProperties().put(HttpSession.class.getName(),HttpSession);
}
@凌驾
公共T getEndpointInstance(类endpointClass)引发实例化异常{
//TODO获取协商的子策略,并使用开关盒或工厂实例化端点
返回(T)新的WebSocketControllerA();
//或返回(T)新的WebSocketControllerB();
//或返回(T)新的WebSocketControllerC();
// ...
}
}
你知道如何解决这个问题吗?或者有没有被广泛接受的方法来处理不同的子程序?我很难在web上找到有关子程序处理的示例实现或高级文档。这就是您要找的吗
@ServerEndpoint("/ws")
public class MyWebSocket {
@OnOpen
public void onOpen(Session session) {
session.getNegotiatedSubprotocol();
}