我正在尝试在我的应用程序中实现一些websocket的东西。我使用Spring Boot和Groovy来实现这一点。但我有一些麻烦与websocket消息发送,但没有正确处理的Spring控制器。我试着搜索同样的问题,但没有找到任何有用的信息。
目前,为了简化,我使用了与这里相同的类:https://github.com/spring-guides/gs-messaging-stomp-websocket
当我运行我的应用程序时,在控制台中我看到:
INFO 3778—[main] s.a.s.SimpAnnotationMethodMessageHandler:映射"{[/hello/**],messageType=[MESSAGE]}"到public org.myapp.Greeting . org.myapp.GreetingController.greeting(org.myapp.HelloMessage)抛出java.lang.Exception
所以看起来不错。但事实并非如此。控制器代码:
@Controller
class GreetingController {
@MessageMapping("/hello/**")
@SendTo("/topic/greetings")
Greeting greeting(HelloMessage message) throws Exception {
return new Greeting("Hello, " + message.getName() + "!")
}
}
和我的websocket配置是:
@Configuration
@EnableWebSocketMessageBroker
class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic")
config.setApplicationDestinationPrefixes("/app")
}
@Override
void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/hello").withSockJS()
}
}
我可以连接到上面的websocket,但发送任何消息都不会产生任何效果。我正在使用以下javascript代码:
var stompClient = null;
var socket = new SockJS('/hello');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/greetings', function (greeting) {
console.log('received message');
console.log(greeting);
});
stompClient.send("app/hello", {}, JSON.stringify({'name': 'my name'}));
});
发送消息但没有回复。我在控制器方法中添加了打印消息它不打印任何东西所以发送的问候消息没有传递给控制器方法,它甚至没有被调用。我甚至尝试设置映射来处理所有消息@MessageMapping('/**')
,但它不起作用。
这是调用以上javascript代码后的Spring日志(调试级别):
2015-09-02 21:47:49.593 DEBUG 3778—[nio-8080-exec-5] o.s.web.servlet.DispatcherServlet: DispatcherServlet with name ' DispatcherServlet '处理GET请求[/hello/info]2015-09-02 21:47:49.593 DEBUG 3778—[nio-8080- exex -5] s.w.s.m.m.a.RequestMappingHandlerMapping:查找路径/hello/info的处理方法2015-09-02 21:47:49.593 DEBUG 3778—[nio-8080- exex -5] s.w.s.m.m.a.RequestMappingHandlerMapping: Did not find handler method for [/hello/info]2015-09-02 21:47:49.593 DEBUG 3778—[nio-8080- exx -5] o.s.w.s.handler.SimpleUrlHandlerMapping:请求[/hello/info]的匹配模式是[/hello/]2015-09-02 21:47:49.593 DEBUG 3778—[nio-8080- exc -5] o.s.w.s.handler.SimpleUrlHandlerMapping:请求[/hello/info]的URI模板变量是{}2015-09-02 21:47:49.593 DEBUG 3778—[nio-8080- exc -5] o.s.w.s.handler.SimpleUrlHandlerMapping:映射[/hello/info]到HandlerExecutionChain的处理程序[org.springframework.web.socket.sockjs.support]。SockJsHttpRequestHandler@31e2232f]和1个拦截器2015-09-02 21:47:49.593 DEBUG 3778—[nio-8080-exec-5] o.s.web.servlet.DispatcherServlet: Last-Modified value for [/hello/info]为:-12015-09-02 21:47:49.594 DEBUG 3778—[nio-8080-exec-5] o.s.w.s.s.t.h.DefaultSockJsService: GET http://localhost:8080/hello/info?t=14412232695612015-09-02 21:47:49.594 DEBUG 3778—[nio-8080-exec-5] o.s.web.servlet.DispatcherServlet: Null ModelAndView返回给DispatcherServlet,名称为' DispatcherServlet ':假设HandlerAdapter完成请求处理2015-09-02 21:47:49.594 DEBUG 3778—[nio-8080-exec-5] o.s.web.servlet.DispatcherServlet:成功完成请求2015-09-02 21:47:49.600 DEBUG 3778—[nio-8080-exec-8] o.s.web.servlet.DispatcherServlet: DispatcherServlet with name ' DispatcherServlet '处理GET请求[/hello/721/4hmzc45f/websocket]2015-09-02 21:47:49.601 DEBUG 3778—[nio-8080- exc -8] s.w.s.m.m.a.RequestMappingHandlerMapping:查找路径/hello/721/4hmzc45f/websocket的处理程序方法2015-09-02 21:47:49.601 DEBUG 3778—[nio-8080- exex -8] s.w.s.m.m.a.RequestMappingHandlerMapping: Did not find handler method for [/hello/721/4hmzc45f/websocket]2015-09-02 21:47:49.601 DEBUG 3778—[nio-8080- exx -8] o.s.w.s.handler.SimpleUrlHandlerMapping:请求[/hello/721/4hmzc45f/websocket]的匹配模式是[/hello/]2015-09-02 21:47:49.601 DEBUG 3778—[nio-8080- exx -8] o.s.w.s.handler.SimpleUrlHandlerMapping:请求[/hello/721/4hmzc45f/websocket]的URI模板变量为{}2015-09-02 21:47:49.601 DEBUG 3778—[nio-8080- exc -8] o.s.w.s.handler.SimpleUrlHandlerMapping:映射[/hello/721/4hmzc45f/websocket]到HandlerExecutionChain,使用handler [org.springframework.web.socket.sockjs.support]。SockJsHttpRequestHandler@31e2232f]和1个拦截器2015-09-02 21:47:49.601 DEBUG 3778—[nio-8080-exec-8] o.s.web.servlet.DispatcherServlet:上次修改[/hello/721/4hmzc45f/websocket]的值为:-12015-09-02 21:47:49.603 DEBUG 3778—[nio-8080-exec-8] o.s.web.servlet.DispatcherServlet: Null ModelAndView返回给DispatcherServlet,名称为' DispatcherServlet ':假设HandlerAdapter完成请求处理2015-09-02 21:47:49.603 DEBUG 3778—[nio-8080-exec-8] o.s.web.servlet.DispatcherServlet:成功完成请求2015-09-02 21:47:49.604 DEBUG 3778—[nio-8080- exc -8] s.w.s.h.LoggingWebSocketHandlerDecorator: New WebSocketServerSockJsSession[id=4hmzc45f]
我尝试使用相同的依赖(我使用Gradle,如果这是重要的)和类gs-messaging-stomp-websocket (https://github.com/spring-guides/gs-messaging-stomp-websocket),但没有帮助。
我不知道这里发生了什么,为什么代码不工作。
我将感激任何帮助。提前谢谢。
几个小时后,我终于找到了一个解决方案…在javascript代码中,我发布了我正在发送消息到
" app/hello">
代替
"/app/hello">
不应该是stompClient.send("app/hello", {}, JSON.stringify({'name': 'my name'}));
,应该是stompClient.send("/app/hello", {}, JSON.stringify({'name': 'my name'}));
如此简单和愚蠢的东西,但很难找到…也许有人会有同样的问题,并找到这个解决方案,所以我没有删除我的问题。