0

我正在尝试使用 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);

也许stomp无法连接?

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) 函数时,我无法捕获该断点。函数永远不会被调用。

你知道我在做什么错吗?

4

1 回答 1

0

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

于 2015-11-30T18:57:08.430 回答