我有一个 Web 服务在独立应用程序中的 Grizzly 之上运行。
Grizzly HTTP 服务器实例化如下:
this.httpServer = GrizzlyHttpServerFactory.createHttpServer(uri);
WebappContext applicationContext = new WebappContext("test", "/test");
ServletRegistration registration = applicationContext.addServlet("jersey", ServletContainer.class);
registration.addMapping("/*");
registration.setInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, MyApplicationService.class.getName());
applicationContext.deploy(httpServer);
WebSocketAddOn webSocketAddon = new WebSocketAddOn();
httpServer.getListeners().forEach(listener -> {
listener.registerAddOn(webSocketAddon);
});
WebSocketEngine.getEngine().register("/ws", "/notifications", myNotificationService);
问题:我的通知服务不接受任何 Web 套接字连接。
但是如果我改变:
// factory from org.glassfish.jersey.grizzly2.httpserver
this.httpServer = GrizzlyHttpServerFactory.createHttpServer(uri);
作者:
// factory from org.glassfish.grizzly.http.server
this.httpServer = HttpServer.createSimpleServer(null, 8084);
然后我的服务确实接受 Web 套接字连接。
我不明白为什么。两者有什么区别?
最佳答案
它必须很简单......
更换
this.httpServer = GrizzlyHttpServerFactory.createHttpServer(uri);
由
this.httpServer = GrizzlyHttpServerFactory.createHttpServer(uri, false);
解决问题。
HTTP 服务器在有机会使用 Web Socket 插件进行配置之前已自动启动。
关于java - grizzly + jersey + websocket - 不接受连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46130825/