Spring Websocket Handler not publishing message to all clients

huangapple go评论77阅读模式
英文:

Spring Websocket Handler not publishing message to all clients

问题

我有一个非常简单的Spring Websocket服务器:

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry.addHandler(new WebsocketHandler(), "/socket").setAllowedOrigins("*");
    }
    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        container.setMaxBinaryMessageBufferSize(1024000);
        container.setMaxTextMessageBufferSize(1024000);
        return container;
    }
}

public class WebsocketHandler extends AbstractWebSocketHandler {
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {
        System.out.println(message.getPayload() + "\n");
        session.sendMessage(message);
    }

    @Override
    protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws IOException {
        System.out.println("New Binary Message Received");
        session.sendMessage(message);
    }
}

当我打开一个仅包含SockJS库的简单HTML客户端时,客户端可以从服务器发送和接收消息,但如果我打开一个新标签并使用类似的客户端,它无法接收其他客户端发布的消息。

这是因为session.sendMessage(message);吗?在一个客户端发布消息时,所有其他客户端都能接收到的配置应该是什么样的?

英文:

I have a Spring Websocket server, very simple one:

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry.addHandler(new WebsocketHandler(), "/socket").setAllowedOrigins("*");
    }
    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        container.setMaxBinaryMessageBufferSize(1024000);
        container.setMaxTextMessageBufferSize(1024000);
        return container;
    }

}

And

public class WebsocketHandler extends AbstractWebSocketHandler {
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {
        System.out.println(message.getPayload()+"\n");
        session.sendMessage(message);
    }

    @Override
    protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws IOException {
        System.out.println("New Binary Message Received");
        session.sendMessage(message);
    }
}

When I open the client which is just a simple HTML with SockJS library, the client can send receive messages from the server but if I open a new tab with the similar client it is not able to receive the messages posted by the other client.

Is this because of session.sendMessage(message);? What should be the configuration that when one client posts messages all other clients would be able to receive it?

答案1

得分: 1

如果您想将数据发送给其他客户端,您需要跟踪会话。正如您可以在此链接中的文档中看到,处理程序还具有在连接打开后和连接关闭后两个函数。那么这如何帮助您呢?
您可以保持一个同步的集合,比如CopyOnWriteList<T>,或者使用Collections.Synchronized,当客户端连接(afterConnectionEstablished(WebSocketSession session))时,将该客户端添加到这个集合中,或者当客户端断开连接(afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus))时,可以从集合中移除会话。当您想要向每个客户端发送消息时,遍历这个集合并使用会话发送对象。如果您仍然觉得这很混淆,我可以提供一个代码示例。

英文:

if you want to send the data to other clients, you need to track sessions. As you can see in the document at this LINK the handler also has two functions for after connection is open and after the connection is closed. So how does this help you?
You can keep a synchronized collection like CopyOnWriteList&lt;T&gt; or use Collections.Synchronized, when a client connects (afterConnectionEstablished(WebSocketSession session)), add this client to this collection or when the client disconnects ( afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) ), you can remove the session from your collection. when you want to send a message to every client, loop over this collection and use the session to send Objects. if you still find this confusing, I can provide a code sample.

huangapple
  • 本文由 发表于 2020年4月7日 11:01:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/61072137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定