# Spring Session - WebSocket
本指南描述了如何使用 Spring Session 来确保 WebSocket 消息使你的 HttpSession 保持活跃。
Spring session 的 WebSocket 支持仅对 Spring 的 WebSocket 支持有效。 具体来说,它不能直接使用JSR-356 (opens new window),因为 JSR-356 没有拦截传入 WebSocket 消息的机制。 |
---|
# HttpSession 设置
第一步是将 Spring Session 与 HttpSession 集成在一起。这些步骤已经在HttpSession with Redis 指南中进行了概述。
在继续之前,请确保你已经将 Spring Session 集成到 HttpSession 中。
# Spring 配置
在典型的 Spring WebSocket 应用程序中,你将实现WebSocketMessageBrokerConfigurer
。例如,配置可能如下所示:
@Configuration
@EnableScheduling
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/messages").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/queue/", "/topic/");
registry.setApplicationDestinationPrefixes("/app");
}
}
我们可以更新配置以使用 Spring Session 的 WebSocket 支持。下面的示例展示了如何做到这一点:
SRC/main/java/samples/config/websocketconfig.java
@Configuration
@EnableScheduling
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractSessionWebSocketMessageBrokerConfigurer<Session> { (1)
@Override
protected void configureStompEndpoints(StompEndpointRegistry registry) { (2)
registry.addEndpoint("/messages").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/queue/", "/topic/");
registry.setApplicationDestinationPrefixes("/app");
}
}
要连接 Spring Session 支持,我们只需要更改两件事:
1 | 而不是实现WebSocketMessageBrokerConfigurer ,我们扩展AbstractSessionWebSocketMessageBrokerConfigurer |
---|---|
2 | 我们将registerStompEndpoints 方法重命名为configureStompEndpoints |
AbstractSessionWebSocketMessageBrokerConfigurer
在幕后做什么?
WebSocketConnectHandlerDecoratorFactory
作为WebSocketHandlerDecoratorFactory
添加到WebSocketTransportRegistration
。这确保了一个包含WebSocketSession
的自定义SessionConnectEvent
被触发。当 Spring Session 结束时,要结束任何仍处于打开状态的 WebSocket 连接,WebSocketSession
是必需的。SessionRepositoryMessageInterceptor
作为HandshakeInterceptor
添加到每个StompWebSocketEndpointRegistration
。这确保将Session
添加到 WebSocket 属性中,以允许更新上次访问的时间。SessionRepositoryMessageInterceptor
作为ChannelInterceptor
添加到我们的入站ChannelRegistration
中。这确保了每次接收入站消息时,都会更新我们 Spring Session 的最后一次访问时间。WebSocketRegistryListener
被创建为 Spring Bean。这确保了我们将所有Session
ID 映射到相应的 WebSocket 连接。通过维护此映射,我们可以在 Spring Session 结束时关闭所有 WebSocket 连接。
# websocket
示例应用程序
websocket
示例应用程序演示了如何在 WebSockets 中使用 Spring Session 。
# 运行websocket
示例应用程序
你可以通过获取源代码 (opens new window)并调用以下命令来运行示例:
$ ./gradlew :spring-session-sample-boot-websocket:bootRun
为了测试会话过期,你可能希望在启动应用程序之前添加以下配置属性,从而将会话过期时间更改为 1 分钟(默认为 30 分钟): SRC/main/resources/application.properties <br/>server.servlet.session.timeout=1m # Session timeout. If a duration suffix is not specified, seconds will be used.<br/> |
---|
要使示例工作,你必须在 localhost 上安装 Redis2.8+ (opens new window)并使用默认端口(6379)运行它。 或者,你可以更新 RedisConnectionFactory 以指向 Redis 服务器。另一个选项是使用Docker (opens new window)在 localhost 上运行 Redis。 有关详细说明,请参见Docker Redis 存储库 (opens new window)。 |
---|
现在你应该可以在http://localhost:8080/ (opens new window)上访问应用程序了。
# 探索websocket
示例应用程序
现在你可以尝试使用该应用程序了。使用以下信息进行身份验证:
**用户 Name ** 罗布
密码 密码
现在点击登录按钮。你现在应该被验证为用户罗布。
打开一个隐身窗口并访问http://localhost:8080/ (opens new window)
系统会提示你输入登录表单。使用以下信息进行身份验证:
**用户 Name ** 卢克
密码 密码
现在把罗布的话传给卢克。消息应该会出现。
等两分钟,再试着把罗布的信息发送给卢克。你可以看到该消息已不再发送。
为什么是两分钟? Spring Session 在 60 秒内到期,但是来自 Redis 的通知不能保证在 60 秒内发生。 以确保套接字在合理的时间内关闭, Spring Session 在 00 秒时每分钟运行一个后台任务,该任务强制清除任何过期的会话。 这意味着在关闭 WebSocket 连接之前,你最多需要等待两分钟。 |
---|
你现在可以尝试访问http://localhost:8080/ (opens new window),提示你再次进行身份验证。这表明会话正确地过期了。
现在重复同样的练习,但不是等待两分钟,而是每 30 秒发送一条来自每个用户的消息。你可以看到消息继续被发送。尝试访问http://localhost:8080/ (opens new window),没有提示你再次进行身份验证。这表明会话是保持活力的。
只有来自用户的消息才能使会话保持活跃。 这是因为只有来自用户的消息才意味着用户活动。 收到的消息并不意味着活动,因此不会更新会话过期时间。 |
---|