[英]Spring 4 STOMP Websockets Heartbeat
I can't seem to find a good resource on how to send heartbeats to clients using websockets in Spring! 我似乎找不到如何在Spring中使用websockets向客户端发送心跳的好资源!
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 is the client code used to communicate with the server: 这是用于与服务器通信的客户端代码:
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. 我真的想实现心跳,以便客户端知道谁连接并发送一些数据以保持客户端和服务器同步。
Do I need to manually do it? 我需要手动操作吗?
The Spring SockJS configuration contains settings for sending heartbeats. Spring SockJS配置包含发送心跳的设置。 By default a heartbeat is sent every 25 seconds assuming no other messages are sent on the connection.
默认情况下,假设连接上没有发送其他消息,则每25秒发送一次心跳。 See the Spring reference for details.
有关详细信息,请参阅Spring参考 。
Just call: 只需致电:
.setTaskScheduler(heartBeatScheduler());
for the broker config where you want to enable it (works with simple broker too). 对于要启用它的代理配置(也适用于简单代理)。
@Configuration
public class WebSocketMessageBrokerConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.setApplicationDestinationPrefixes("/app");
config.enableSimpleBroker("/topic", "/queue", "/user")
.setTaskScheduler(heartBeatScheduler());
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/endpoint");
}
@Bean
public TaskScheduler heartBeatScheduler() {
return new ThreadPoolTaskScheduler();
}
}
For simple broker you can config heartbeat like this: 对于简单的代理,您可以像这样配置心跳:
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/wshandler" allowed-origins="*">
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic, /queue" heartbeat="10000,10000" scheduler="pingScheduler"/>
</websocket:message-broker>
<bean id="pingScheduler" class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
<property name="poolSize" value="1"/>
<property name="threadNamePrefix" value="wss-heartbeat-thread-"/>
</bean>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.