代码之家  ›  专栏  ›  技术社区  ›  watery

如何检测SpringWebSocketStomp订阅消息(帧)?

  •  0
  • watery  · 技术社区  · 6 年前

    我用的是弹簧5:如何检测 SUBSCRIBE 来自Stomp客户端的消息?

    根据我的理解, @SubscribeMapping 应该使我的控制器方法在客户机订阅主题时被调用,但这并没有发生。

    这是我的服务器控制器:

    @Controller
    public class MessageController {
    
        // ...
    
        @MessageMapping("/chat/{mId}")
        @SendTo("/topic/messages")
        public OutputMessage send(Message message, @DestinationVariable("mId") String mid, MessageHeaders headers, MessageHeaderAccessor accessor) throws Exception {
            // ...
        }
    
        @SuppressWarnings("unused")
        @SubscribeMapping({ "/", "/chat", "/topic/messages", "/messages", "/*" })
        public void listen(Message message, MessageHeaders headers, MessageHeaderAccessor accessor) throws Exception {
            int i = 0;
            System.out.println("subscribed");
        }
    
    }
    

    服务器配置:

    @Configuration
    @ComponentScan(basePackages= { "websockets" })
    @EnableWebSocketMessageBroker
    public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    
        @Override
        public void configureMessageBroker(MessageBrokerRegistry config) {
            config.enableSimpleBroker("/topic");
            config.setApplicationDestinationPrefixes("/app");
        }
    
        @Override
        public void registerStompEndpoints(StompEndpointRegistry registry) {
             registry.addEndpoint("/chat");
             registry.addEndpoint("/chat").withSockJS();
        }
    
        @Override
        public void configureWebSocketTransport(WebSocketTransportRegistration registry) {
            WebSocketMessageBrokerConfigurer.super.configureWebSocketTransport(registry);
        }
    }
    

    以及javascript客户端:

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8" />
            <title>Chat WebSocket</title>
            <script src="sockjs.js"></script>
            <script src="stomp.js"></script>
    
            <script type="text/javascript">
    
                // ...
    
                function connect() {
                    var sock = new SockJS('/<webapp-context>/chat');
    
                    stompClient = Stomp.over(sock);  
                    stompClient.connect({}, function(frame) {
                        setConnected(true);
                        console.log('Connected: ' + frame);
                        stompClient.subscribe('/topic/messages', function(messageOutput) {
                            showMessageOutput(JSON.parse(messageOutput.body));
                        });
                        stompClient.subscribe('/topic/messages/13', function(messageOutput) {
                            showMessageOutput(JSON.parse(messageOutput.body));
                        });
                    });
                }
    
                // ...
    
            </script>
        </head>
        <body onload="/*disconnect()*/">
    
            <!-- ... -->
    
        </body>
    </html>
    

    代码改编自 Intro to WebSockets with Spring .

    如在 this answer in the docs 我可以使用拦截器,但是如何 @SubscribeMapping 然后工作?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Marcus Held    6 年前

    您还需要将“主题”注册为应用程序目标主题 config.setApplicationDestinationPrefixes({"/app", "/topic"}); .

    否则,Spring不会将订阅消息转发到应用程序,而是将其转发到消息代理通道。

    推荐文章