我们有一个Spring over WebSockets连接,我们正在传递一个CONNECT
框架:
CONNECT\naccept-version:1.2\nheart-beat:10000,10000\n\n\u0000
处理程序确认哪个,启动一个新会话,然后返回:
CONNECTED
version:1.2
heart-beat:0,0
但是,我们想要心跳,以便我们可以保持WebSocket开放。我们没有使用SockJS。
我介绍了Spring Message Handler:
StompHeaderAccessor [headers={simpMessageType=CONNECT, stompCommand=CONNECT, nativeHeaders={accept-version=[1.2], heart-beat=[5000,0]}, simpSessionAttributes={}, simpHeartbeat=[J@5eba717, simpSessionId=46e855c9}]
获得heart-beat
(本机标题)后,它设置的内存地址为simpHeartbeat=[J@5eba717, simpSessionId=46e855c9}]
值得注意的是,经纪人验证后:
Processing CONNECT session=46e855c9
(这里的sessionId与simpSessionId不同)?
当运行早期的TRACE
调试时,我看到了一个通知“调度心跳......”或者那种效果......虽然我现在没有看到它?
知道发生了什么事吗?
谢谢
我在documentation找到了解释:
来自SockJS任务调度程序的线程池的SockJS任务计划程序统计信息,用于发送检测信号。请注意,在STOMP级别协商心跳时,将禁用SockJS心跳。
SockJS的心跳是否与STOMP心跳不同?
是的SockJS心跳是不同的。基本上相同的事情,但他们在SockJS协议中的目的是确保连接看起来不像它“死”,在这种情况下代理可以主动关闭它。更一般地,心跳允许每一方主动地检测连接问题并清理资源。
在传输层使用STOMP和SockJS时,如果正在使用STOMP心跳,则不需要同时关闭SockJS心跳的原因。但是你没有在这里使用SockJS。
您没有显示任何配置,但我的猜测是您正在使用不会自动发送心跳的内置简单代理。配置时,您将看到启用心跳的选项,还需要设置任务计划程序。
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
// ...
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableStompBrokerRelay(...)
.setTaskScheduler(...)
.setHeartbeat(...);
}
}
从Spring 4.2开始,您可以使用Stomp over SockJS和内置的SimpleBroker从服务器端完全控制心跳协商结果:
public class WebSocketConfigurer extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
ThreadPoolTaskScheduler te = new ThreadPoolTaskScheduler();
te.setPoolSize(1);
te.setThreadNamePrefix("wss-heartbeat-thread-");
te.initialize();
config.enableSimpleBroker("/")
/**
* Configure the value for the heartbeat settings. The first number
* represents how often the server will write or send a heartbeat.
* The second is how often the client should write. 0 means no heartbeats.
* <p>By default this is set to "0, 0" unless the {@link #setTaskScheduler
* taskScheduler} in which case the default becomes "10000,10000"
* (in milliseconds).
* @since 4.2
*/
.setHeartbeatValue(new long[]{heartbeatServer, heartbeatClient})
.setTaskScheduler(te);
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint(.....)
.setAllowedOrigins(....)
.withSockJS();
}
}
我们遇到了Spring,Websockets,STOMP和Spring Sessions的相同问题 - 没有heartbeats和Spring会话可能会过期而websocket不会在服务器端接收消息。我们最终每隔20000ms从浏览器启用STOMP心跳,并将SimpMessageType.HEARTBEAT添加到Spring sessionRepositoryInterceptor匹配,以保持Spring会话最后访问时间在STOMP心跳上更新而不消息。我们必须使用AbstractSessionWebSocketMessageBrokerConfigurer作为基础来启用内置Spring会话和websocket会话绑定。 Spring manual,第二个例子。在官方示例中,Spring会话更新了入站websocket CONNECT / MESSAGE / SUBSCRIBE / UNSUBSCRIBE消息,但不是心跳,这就是为什么我们需要重新配置2件事 - 至少启用入站心跳并调整Spring会话以响应websocket心跳
public class WebSocketConfig extends AbstractSessionWebSocketMessageBrokerConfigurer<ExpiringSession> {
@Autowired
SessionRepositoryMessageInterceptor sessionRepositoryInterceptor;
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
sessionRepositoryInterceptor.setMatchingMessageTypes(EnumSet.of(SimpMessageType.CONNECT,
SimpMessageType.MESSAGE, SimpMessageType.SUBSCRIBE,
SimpMessageType.UNSUBSCRIBE, SimpMessageType.HEARTBEAT));
config.setApplicationDestinationPrefixes(...);
config.enableSimpleBroker(...)
.setTaskScheduler(new DefaultManagedTaskScheduler())
.setHeartbeatValue(new long[]{0,20000});
}
}
我们尝试的另一种方法是重新实现SessionRepositoryMessageInterceptor功能,以更新Spring会话对出站websocket消息的最后访问时间,并通过侦听器维护websocket session-> Spring会话映射,但上面的代码可以解决问题。