1
votes

I'm trying to implement some websocket stuff in my app. I'm using Spring Boot and Groovy to achieve that. But I have some troubles with websocket messages that are sent but not handled correctly by Spring controller. I tried to search for the same issue but didn't find any useful information.

Currently, to simplify I used the same classes as here: https://github.com/spring-guides/gs-messaging-stomp-websocket

When I run my application, in console I see this:

INFO 3778 --- [ main] s.a.s.SimpAnnotationMethodMessageHandler : Mapped "{[/hello/**],messageType=[MESSAGE]}" onto public org.myapp.Greeting org.myapp.GreetingController.greeting(org.myapp.HelloMessage) throws java.lang.Exception

so it looks good. But in fact, it isn't. My controller code:

@Controller
class GreetingController {

@MessageMapping("/hello/**")
@SendTo("/topic/greetings")
Greeting greeting(HelloMessage message) throws Exception {
  return new Greeting("Hello, " + message.getName() + "!")
}

}

and my websocket configuration is:

@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()
  }

}

I can connect to above websocket but sending any messages doesn't give any effects. I'm doing it with the following javascript code:

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'}));
});

It sends messages but there is no reply. I added printing messages in controller method and it doesn't print anything so sent greeting message isn't passed to the controller method, it isn't even invoked. I tried even setting mapping to handle all messages @MessageMapping('/**') but it didn't work.

This is Spring log (debug level) after invoking above javascript code:

2015-09-02 21:47:49.593 DEBUG 3778 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/hello/info] 2015-09-02 21:47:49.593 DEBUG 3778 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /hello/info 2015-09-02 21:47:49.593 DEBUG 3778 --- [nio-8080-exec-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-exec-5] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/hello/info] are [/hello/] 2015-09-02 21:47:49.593 DEBUG 3778 --- [nio-8080-exec-5] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/hello/info] are {} 2015-09-02 21:47:49.593 DEBUG 3778 --- [nio-8080-exec-5] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/hello/info] to HandlerExecutionChain with handler [org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler@31e2232f] and 1 interceptor 2015-09-02 21:47:49.593 DEBUG 3778 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/hello/info] is: -1 2015-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=1441223269561 2015-09-02 21:47:49.594 DEBUG 3778 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling 2015-09-02 21:47:49.594 DEBUG 3778 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : Successfully completed request 2015-09-02 21:47:49.600 DEBUG 3778 --- [nio-8080-exec-8] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/hello/721/4hmzc45f/websocket] 2015-09-02 21:47:49.601 DEBUG 3778 --- [nio-8080-exec-8] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /hello/721/4hmzc45f/websocket 2015-09-02 21:47:49.601 DEBUG 3778 --- [nio-8080-exec-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-exec-8] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/hello/721/4hmzc45f/websocket] are [/hello/] 2015-09-02 21:47:49.601 DEBUG 3778 --- [nio-8080-exec-8] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/hello/721/4hmzc45f/websocket] are {} 2015-09-02 21:47:49.601 DEBUG 3778 --- [nio-8080-exec-8] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/hello/721/4hmzc45f/websocket] to HandlerExecutionChain with handler [org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler@31e2232f] and 1 interceptor 2015-09-02 21:47:49.601 DEBUG 3778 --- [nio-8080-exec-8] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/hello/721/4hmzc45f/websocket] is: -1 2015-09-02 21:47:49.603 DEBUG 3778 --- [nio-8080-exec-8] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling 2015-09-02 21:47:49.603 DEBUG 3778 --- [nio-8080-exec-8] o.s.web.servlet.DispatcherServlet : Successfully completed request 2015-09-02 21:47:49.604 DEBUG 3778 --- [nio-8080-exec-8] s.w.s.h.LoggingWebSocketHandlerDecorator : New WebSocketServerSockJsSession[id=4hmzc45f]

I tried to use the same dependencies (I'm using Gradle if that's important) and classes as gs-messaging-stomp-websocket (https://github.com/spring-guides/gs-messaging-stomp-websocket) but didn't help.

I don't have any idea what's going on here and why isn't that code working.

I would appreciate any help. Thanks in advance.

1

1 Answers

2
votes

After many hours I finally found a solution...In javascript code I posted I was sending message to

"app/hello"

instead of

"/app/hello"

so it shouldn't be stompClient.send("app/hello", {}, JSON.stringify({'name': 'my name'})); but stompClient.send("/app/hello", {}, JSON.stringify({'name': 'my name'}));

So simple and stupid thing but so hard to find...maybe somebody will have the same problem and find this solution so I don't delete my question.