Spring 4 STOMP Websockets Heartbeat [英] Spring 4 STOMP Websockets Heartbeat
问题描述
我似乎找不到如何在Spring中使用websockets向客户端发送心跳的好资源!
I can't seem to find a good resource on how to send heartbeats to clients using websockets in Spring!
我有一个使用此配置运行的基本服务器:
I have a basic server running using this configuration:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/room");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/channels").withSockJS();
}
}
然后我用这样的东西向人们发送消息谁订阅了房间:
Then I use something like this to send messages to people who subscribed to a room:
this.simpMessagingTemplate.convertAndSend("/room/" + this.roomId, message);
这是用于与服务器通信的客户端代码:
this.connect = function (roomNameParam, connectionCallback) {
var socket = new SockJS('http://localhost:8080/channels'),
self.stompClient = Stomp.over(socket);
self.stompClient.connect({}, function (frame) {
self.stompClient.subscribe('/room/' + roomNameParam, connectionCallback);
});
};
我真的想实现心跳,以便客户端知道谁连接并发送一些数据来保持心跳客户端和服务器同步。
I really want to implement heartbeats so the client knows who is connected and to send some data to keep the client and server in sync.
我需要手动完成吗?
推荐答案
春天SockJS配置包含发送心跳的设置。默认情况下,假设连接上没有发送其他消息,则每25秒发送一次心跳。请参阅春天参考了解详情。
The Spring SockJS configuration contains settings for sending heartbeats. By default a heartbeat is sent every 25 seconds assuming no other messages are sent on the connection. See the Spring reference for details.
这篇关于Spring 4 STOMP Websockets Heartbeat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!