我已经设法让默认的tomcat websocket监听器通过spring boot grails应用程序工作:
它记录在这里:
https://github.com/vahidhedayati/testwebsocket-grails3
我决定更新这篇文章,并包括到目前为止我在这个问题上的所有发现。
更具体地说,应用程序 grails-app/init 文件夹中的 application.groovy:
这个 bean 启动默认的 tomcat websocket 监听器:
@Bean
public ServletListenerRegistrationBean<AnotherWebSocketHandler> httpSessionEventPublisher() {
return new ServletListenerRegistrationBean<AnotherWebSocketHandler>(new AnotherWebSocketHandler());
}
在搞乱插件重用的同时,发现是:
上面的项目是一个基本的 grails 应用程序,它做了两件事,一个基本的 spring socket 以及 java 1.X Websocket:
这是在 grails 3 插件中使用默认 websocket 的方法
在你的插件描述符中,你有这样的东西:
Closure doWithSpring() {
{->
wsChatConfig DefaultWsChatConfig
}
}
在这个插件中,我留下了两种启动监听器的方法:
@Bean
public ServletContextInitializer myInitializer() {
return new ServletContextInitializer() {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.addListener(WsCamEndpoint)
servletContext.addListener(WsChatFileEndpoint)
}
}
}
// Alternative way
@Bean
public ServletListenerRegistrationBean<WsChatEndpoint> httpSessionEventPublisher() {
return new ServletListenerRegistrationBean<WsChatEndpoint>(new WsChatEndpoint())
}
top 方法非常方便,因为您只能初始化 1 个 ServletListenerRegistrationBean,而我不得不求助于 top 方法来启用其他侦听器……我本可以将 top primary 用于所有调用。留作日后参考。。
有了这个,spring boot 现在可以模拟 web.xml 在注册监听器时的模拟。从那里加载 websockets 的实际 groovy 类就像它们一样使用默认 websocket 调用,例如 onOpen onMessage 等。