Search code examples
grailsspring-websocket

Spring Websocket Plugin return value


I'm using the Spring Websocket Plugin and I want to send some logging-output to the client.

Here is my Code:

@MessageMapping("/run")
@SendTo("/topic/run")
protected String run(@Payload WebForm form) {
      doSomeWithForm(form)
    return "started"
}

GSP:

    $(function () {
        var socket = new SockJS("${createLink(uri: '/stomp')}");
        var client = Stomp.over(socket);

        client.connect({}, function () {
            client.subscribe("/topic/run", function (message) {
                $("#log").append(message.body);
            });
        });
        $("#submitBtn").click(function() {
            client.send("/app/run", {}, JSON.stringify({
                'type':$("#type").val(),
                'id':$("#id").val()
            }));
        });
    });

This works well so far, but message.body looks like this ""This my return text"".

Why are there two "? How can I extract this?


Solution

  • try JSON.parse(message.body).

    note: that behavior will change with grails-2.5/grails-3.0 shipping spring-4.1+. from that spring version on, plain String payloads will not be json-encoded anymore, meaning then the message.body in your js code will have the string value without double quoting