tomcat - 设置(Websocket)ServerEndpoint 配置器没有注释

标签 tomcat jakarta-ee websocket

灵感来自 this thread我正在尝试这样做:

@ServerEndpoint(... configurator = GetHttpSessionConfigurator.class)

没有@ServerEndpoint 注解但有这段代码:

ServerEndpointConfig endpointConfig = ServerEndpointConfig.Builder.create(MyHandler.class, uri).build();
javax.websocket.server.ServerContainer.addEndpoint(endpointConfig);

由于找不到任何“setConfigurator”方法,我检查了 ServerEndpointConfig 的来源,发现 Configuration (..tor) 是由

 ServiceLoader.load(Configurator.class)

因此,我创建了一个 meta-inf\services\javax.websocket.server.ServerEndpointConfig$Configurator 文件并添加了一行以指向我的 GetHttpSessionConfigurator(如 1 中定义)类。文件已加载(我使用 Process Monitor 检查过)但使用了 Tomcat 的 DefaultServerEndpointConfig。

有人知道这个设置有什么问题吗?

最佳答案

在你的.build()之前你可以放入一个.configurator(new YourEndPointConfigurator())

因此对于您的代码,您可以执行以下操作:

ServerEndpointConfig endpointConfig = ServerEndpointConfig.Builder
  .create(MyHandler.class, uri)
  .configurator(new ChatServerEndPointConfigurator(new GetHttpSessionConfigurator())
  .build();

更多信息可以在 ServerEndpointConfig.Builder docs 中找到.

关于tomcat - 设置(Websocket)ServerEndpoint 配置器没有注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25913618/

相关文章:

spring-mvc - 使用 Gradle 配置嵌入式 tomcat

java - Spring3, Security3 : System works on windows but not Linux java. lang.IllegalArgumentException异常

java - java.lang.NoSuchMethodError : com. sun.jna.Native.register(Ljava/lang/String;)V 的原因

java - 如何强制创建新的http session

android - 在 OkHttp 中使用安全的 websockets (wss)

java - 如何在 Linux 机器上的 Tomcat 上运行 Web 应用程序时使用 JVM 参数

maven - 如何在 tomcat7-maven-plugin :run? 中使用不同的 web.xml 描述符

java - 如何向不同的网络服务发送多个异步请求?

java - WebSocket 握手期间出错(意外响应代码 : 400)

javascript - 如何在 JavaScript 中定义和接收来自多个 WebSocket 的数据?