繁体   English   中英

如何检测Spring Websocket踩踏订阅消息(框架)?

[英]How to detect Spring websocket stomp subscription messages (frames)?

我正在使用Spring 5:如何检测来自Stomp客户端的SUBSCRIBE消息?

根据我的理解, @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>

使用Spring将代码从Intro修改为WebSockets

该答案文档中所述 ,我只能使用拦截器,但是@SubscribeMapping如何工作?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM