使用Spring消息传递无法通过stomp发送消息

时间:2015-11-29 17:13:33

标签: javascript java spring stomp spring-messaging

我正在尝试使用spring-messaging从STOPM上发送和处理来自浏览器的消息。但是没有发送消息。

现在有些代码:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketStompConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer{

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/marcopolo").withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/queue", "/topic");
        registry.setApplicationDestinationPrefixes("/app");
    }
}

正如您所看到的,我正在使用嵌入式弹簧简单代理。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
    <script th:src="@{/webjars/sockjs-client/1.0.2/sockjs.min.js}"/>
    <script th:src="@{/webjars/stomp-websocket/2.3.3/stomp.min.js}"/>
    <script>
        function doSmth(){
            var sock = new SockJS('/spittr/marcopolo');

            var stomp = Stomp.over(sock);

            var payload = JSON.stringify({'message': 'Marco !'});

            stomp.connect('guest', 'guest', function(frame) {
                stomp.send('/app/marcopolo', {}, payload);
            });
            var dupa = 'dupa';
        }
    </script>
</head>
<body>
    <button onclick="doSmth();">Connect</button>
</body>
</html>

当我尝试调试firefox中的脚本部分时,调试永远不会到达部分:

stomp.send('/app/marcopolo', {}, payload);

也许st脚无法连接?

正确创建stomp和sock对象。

@Controller
public class MarcoController {

    @RequestMapping(value = "/marcop", method = RequestMethod.GET)
    public String getMarcoPolo(){
        return "sockjstest";
    }

    @MessageMapping(value = "/marcopolo")
    public void handleShout(Shout incoming){
        System.out.println(incoming.getMessage());
    }
}

当我调用doSmth()函数并尝试断点handleShout(Shout incoming)函数时,我无法捕获该断点。永远不会调用函数。

你看到我做错了什么吗?

1 个答案:

答案 0 :(得分:0)

这个问题的解决方案真的很容易:) 只是使用了错误的类来扩展我的WebSocketStompConfig。 使用:AbstractSecurityWebSocketMessageBrokerConfigurer 应该是:AbstractWebSocketMessageBrokerConfigurer