zyro23 / grails-spring-websocket

93 stars 28 forks source link

Message gets send twice #50

Closed lorenzoGP closed 7 years ago

lorenzoGP commented 7 years ago

Hi I am getting this strange behavior where my server receives duplicate calls from the client when this calls send: (I am using Sockjs + STOMP for client)

client.send("/app/hello", headers, JSON.stringify({'dateFrom' : "01-04-2017"}));

Code where the connection is established :

socket = new SockJS("http://localhost:8080/websocket");
client = Stomp.over(socket);

client.connect(headers, function() {
        channelMsg = client.subscribe("/user/queue/hello", function(message) {
                          $("#username").val(message.body);
                });
})

Server side:

@Controller
class WebSocketController {

@MessageMapping("/hello")
    @SendToUser("/queue/hello")
    protected String hello(Message<?> message,PagosEvento dates){
    return 'Example: '+dates.dateFrom
    }
}

I would like to know if anyone had stumble with the same issue and how did you guys fixed it ;)

zyro23 commented 7 years ago

snippets look ok i think.. please reference a sample app reproducing the problem.

lorenzoGP commented 7 years ago

I was able to find the problem while preparing the sample app ; ). I have to say that while I found some post about this problem on the internet no one shared a possible solution, so I am glad to share with you what I discovered

Here is what I found :

When I have both of this tags declared

@Controller  <------ 
class WebSocketController {

@MessageMapping("/hello") <------ 
    @SendToUser("/queue/hello")
    protected String hello(Message<?> message,PagosEvento dates){
    return 'Example: '+dates.dateFrom
    }
}

I get duplicates messages send to client, the solution for me was removing @Controller

All the confusion with the tags came following the Spring docs on websockets where they give example as,

@Controller
public class GreetingController {

    @MessageMapping("/greeting") {
    public String handle(String greeting) {
        return "[" + getTimestamp() + ": " + greeting;
    }
}

Hope this solution helps anyone with similar problem

zyro23 commented 7 years ago

ok just to clarify: you added a spring @Controller annotation to a grails controller, right? then the duplicte messages make sense because the spring as well as the grails method resolver would have detected the message handling method.

thank you for the feedback! this one is good to close, i guess?

lorenzoGP commented 7 years ago

Yes, is just as you pointed out!