【问题标题】:Spring: send message to websocket clientsSpring:向 websocket 客户端发送消息
【发布时间】:2015-11-28 23:57:35
【问题描述】:

我正在构建一个使用 Spring Boot、RabbitMQ 和 WebSocket 作为 POC 的网络聊天,但我坚持最后一点:WebSockets
我希望我的 ws 客户端连接到特定端点,例如/room/{id},当有新消息到达时,我希望服务器将响应发送给客户端,但我搜索了类似的东西但没有找到。

目前,当消息到达时,我使用RabbitMQ对其进行处理,例如

container.setMessageListener(new MessageListenerAdapter(){
            @Override
            public void onMessage(org.springframework.amqp.core.Message message, Channel channel) throws Exception {
                log.info(message);
                log.info("Got: "+ new String(message.getBody()));
            }
        });

我想要的是,而不是记录它,我想将它发送给客户端,例如:websocketManager.sendMessage(new String(message.getBody()))

【问题讨论】:

    标签: java spring spring-boot rabbitmq spring-websocket


    【解决方案1】:

    好的,我想我知道了,对于需要它的每个人,这里是答案:

    首先,需要在 pom.xml 中添加 WS 依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-messaging</artifactId>
    </dependency>
    

    创建一个 WS 端点

    @Configuration
    @EnableWebSocketMessageBroker
    public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
    
        @Override
        public void registerStompEndpoints(StompEndpointRegistry registry) {
            // the endpoint for websocket connections
            registry.addEndpoint("/stomp").withSockJS();
        }
    
        @Override
        public void configureMessageBroker(MessageBrokerRegistry config) {
            config.enableSimpleBroker("/");
    
            // use the /app prefix for others
            config.setApplicationDestinationPrefixes("/app");
        }
    
    }
    

    注意:我使用的是 STOMP,所以客户端应该像这样连接

    <script type="text/javascript">
        $(document).ready(function() {
            var messageList = $("#messages");
            // defined a connection to a new socket endpoint
            var socket = new SockJS('/stomp');
            var stompClient = Stomp.over(socket);
            stompClient.connect({ }, function(frame) {
                // subscribe to the /topic/message endpoint
                stompClient.subscribe("/room.2", function(data) {
                    var message = data.body;
                    messageList.append("<li>" + message + "</li>");
                });
    
            });
        });
    </script>
    

    然后,您可以简单地使用

    连接组件上的 ws messenger
    @Autowired
    private SimpMessagingTemplate webSocket;
    

    并使用

    发送消息
    webSocket.convertAndSend(channel, new String(message.getBody()));
    

    【讨论】:

    • “channel”是从哪里来的,作为 convertAndSend 的目标参数?
    • 频道是一个字符串。这是我发送消息的“房间”,类似于"/room.".concat(message.getRoom().getUid().toString())
    猜你喜欢
    • 1970-01-01
    • 2021-07-18
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 2016-02-17
    • 1970-01-01
    相关资源
    最近更新 更多