我正在尝试在Spring Boot项目中使用Javax JSR 356 API实现服务器端Websocket端点。我正在关注this教程。我知道在这个主题上有一些关于SO的问题,但是我找不到可行的解决方案。当我尝试使用客户端(ARC)连接到Websocket时,它仅报告“发生未知错误”。我检查了嵌入式Tomcat服务器的日志,但是没有任何条目。根据Tomcat的启动条目,应用程序的上下文路径为空(“”),因此我确定我使用正确的URI访问websocket。
这是我的代码:
@ServerEndpoint(value = "/socket", configurator = SpringConfigurator.class)
public class WebSocketController
{
@OnOpen
public void onOpen(Session session) throws IOException
{
System.out.println("Socket has been opened: " + session.getId());
}
@OnClose
public void onClose(Session session) throws IonException
{
System.out.println("Socket has been closed: " + session.getId());
}
}
Spring configuration class
@Configuration
public class WebSocketConfig
{
@Bean
public WebSocketController webSocketController()
{
return new WebSocketController();
}
@Bean
public ServletContextAware endpointExporterInitializer(final ApplicationContext applicationContext) {
return new ServletContextAware() {
@Override
public void setServletContext(ServletContext servletContext) {
ServerEndpointExporter serverEndpointExporter = new ServerEndpointExporter();
serverEndpointExporter.setApplicationContext(applicationContext);
try {
serverEndpointExporter.afterPropertiesSet();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
}
}
如何在Spring Boot应用程序中实现服务器端Websocket端点?
更新: 遵循第一个答案后,登录Tomcat服务器。它一开始就停止。
2018-08-16 00:16:37.160 INFO 12944 --- [ main] o.s.w.s.s.s.ServerEndpointExporter : Registering @ServerEndpoint class: class io.ai.vivid.zen.controller.WebSocketController
2018-08-16 00:16:37.169 INFO 12944 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2018-08-16 00:16:37.170 DEBUG 12944 --- [cat-startStop-1] o.apache.catalina.mapper.MapperListener : Unregister host [localhost] at domain [null] for service [StandardService[Tomcat]]
2018-08-16 00:16:37.170 DEBUG 12944 --- [ost-startStop-1] o.apache.catalina.mapper.MapperListener : Unregister Context [] for service [StandardService[Tomcat]]
2018-08-16 00:16:37.170 DEBUG 12944 --- [ost-startStop-1] o.apache.catalina.mapper.MapperListener : Unregister Wrapper [default] in Context [] for service [StandardService[Tomcat]]
2018-08-16 00:16:37.170 DEBUG 12944 --- [ost-startStop-1] o.apache.catalina.mapper.MapperListener : Unregister Wrapper [dispatcherServlet] in Context [] for service [StandardService[Tomcat]]
2018-08-16 00:16:37.170 DEBUG 12944 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Stopping filters
2018-08-16 00:16:37.170 DEBUG 12944 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Stopping filter 'requestContextFilter'
2018-08-16 00:16:37.171 DEBUG 12944 --- [ost-startStop-1] o.a.c.core.ApplicationFilterConfig : JMX de-registration complete for filter of type [org.springframework.boot.web.servlet.filter.OrderedRequestContextFilter] and name [requestContextFilter]
2018-08-16 00:16:37.171 DEBUG 12944 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Stopping filter 'Tomcat WebSocket (JSR356) Filter'
2018-08-16 00:16:37.171 DEBUG 12944 --- [ost-startStop-1] o.a.c.core.ApplicationFilterConfig : JMX de-registration complete for filter of type [org.apache.tomcat.websocket.server.WsFilter] and name [Tomcat WebSocket (JSR356) Filter]
2018-08-16 00:16:37.171 DEBUG 12944 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Stopping filter 'httpPutFormContentFilter'
2018-08-16 00:16:37.171 DEBUG 12944 --- [ost-startStop-1] o.a.c.core.ApplicationFilterConfig : JMX de-registration complete for filter of type [org.springframework.boot.web.servlet.filter.OrderedHttpPutFormContentFilter] and name [httpPutFormContentFilter]
2018-08-16 00:16:37.171 DEBUG 12944 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Stopping filter 'hiddenHttpMethodFilter'
2018-08-16 00:16:37.171 DEBUG 12944 --- [ost-startStop-1] o.a.c.core.ApplicationFilterConfig : JMX de-registration complete for filter of type [org.springframework.boot.web.servlet.filter.OrderedHiddenHttpMethodFilter] and name [hiddenHttpMethodFilter]
2018-08-16 00:16:37.171 DEBUG 12944 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Stopping filter 'characterEncodingFilter'
2018-08-16 00:16:37.171 DEBUG 12944 --- [ost-startStop-1] o.a.c.core.ApplicationFilterConfig : JMX de-registration complete for filter of type [org.springframework.boot.web.servlet.filter.OrderedCharacterEncodingFilter] and name [characterEncodingFilter]
2018-08-16 00:16:37.171 DEBUG 12944 --- [ost-startStop-1] o.a.catalina.session.StandardManager : Stopping
2018-08-16 00:16:37.171 DEBUG 12944 --- [ost-startStop-1] o.a.catalina.session.StandardManager : Unloading persisted sessions
2018-08-16 00:16:37.171 DEBUG 12944 --- [ost-startStop-1] o.a.catalina.session.StandardManager : No persisted sessions to unload
2018-08-16 00:16:37.171 DEBUG 12944 --- [ost-startStop-1] o.apache.catalina.core.StandardContext : Sending application stop events
2018-08-16 00:16:37.172 DEBUG 12944 --- [ost-startStop-1] o.apache.catalina.core.StandardContext : Processing standard container shutdown
2018-08-16 00:16:37.172 DEBUG 12944 --- [ost-startStop-1] org.apache.catalina.loader.WebappLoader : Stopping this Loader
2018-08-16 00:16:37.172 DEBUG 12944 --- [ost-startStop-1] o.a.c.loader.WebappClassLoaderBase : getResourceAsStream(org/apache/catalina/loader/JdbcLeakPrevention.class)
2018-08-16 00:16:37.172 DEBUG 12944 --- [ost-startStop-1] o.a.c.loader.WebappClassLoaderBase : Delegating to parent classloader jdk.internal.loader.ClassLoaders$AppClassLoader@28c97a5
2018-08-16 00:16:37.175 DEBUG 12944 --- [ost-startStop-1] o.a.c.loader.WebappClassLoaderBase : --> Returning stream from parent
2018-08-16 00:16:37.183 DEBUG 12944 --- [ost-startStop-1] o.apache.catalina.core.StandardContext : resetContext Tomcat:j2eeType=WebModule,name=//localhost/,J2EEApplication=none,J2EEServer=none
2018-08-16 00:16:37.183 DEBUG 12944 --- [ost-startStop-1] o.apache.catalina.core.StandardContext : Stopping complete
答案 0 :(得分:2)
我的Websocket配置如下:
@Configuration
@EnableWebSocket
public class WebSocketConfiguration implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
//registry.addHandler(unitWebSocketHandler, "/unit").setAllowedOrigins("*");
}
@Bean
public WebSocketController webSocketController() {
return new WebSocketController();
}
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
然后删除configurator = SpringConfigurator.class
所以您的端点看起来像这样:
@ServerEndpoint(value = "/socket")
public class WebSocketController
该示例项目在GitHub上可用: https://github.com/simasch/spring-boot-websocket