簡體   English   中英

無法使用Spring消息通過踩踏發送消息

[英]Cannot send messages over stomp using Spring messaging

我正在嘗試使用Spring消息通過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");
    }
}

如您所見,我正在使用嵌入式springs簡單代理。

<!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和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傳入)函數時,我無法捕獲該斷點。 函數從不調用。

你看到我在做什么錯嗎?

這個問題的解決方案真的很簡單:)只是使用了錯誤的類來擴展我的WebSocketStompConfig。 使用的:AbstractSecurityWebSocketMessageBrokerConfigurer應該是:AbstractWebSocketMessageBrokerConfigurer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM