WebSocket 集成

Spring Session 提供了与 Spring WebSocket 支持的透明集成。

Spring Session 的 WebSocket 支持仅适用于 Spring 的 WebSocket 支持。具体来说,它不直接与使用 JSR-356 配合使用,因为 JSR-356 没有拦截传入 WebSocket 消息的机制。

为什么选择 Spring Session 和 WebSockets?

那么,当我们使用 WebSockets 时,为什么需要 Spring Session?

考虑一个电子邮件应用程序,它的大部分工作通过 HTTP 请求完成。但是,其中还嵌入了一个通过 WebSocket API 工作的聊天应用程序。如果用户正在与某人积极聊天,我们不应该让 HttpSession 超时,因为这会带来非常糟糕的用户体验。然而,这正是 JSR-356 所做的。

另一个问题是,根据 JSR-356,如果 HttpSession 超时,则所有使用该 HttpSession 和已认证用户创建的 WebSocket 都应被强制关闭。这意味着,如果我们在应用程序中积极聊天并且没有使用 HttpSession,我们也会从对话中断开连接。

WebSocket 用法

WebSocket 示例 提供了一个工作示例,说明如何将 Spring Session 与 WebSockets 集成。您可以按照接下来几个标题中描述的基本集成步骤进行操作,但我们鼓励您在将 Spring Session 与您自己的应用程序集成时,按照详细的 WebSocket 指南进行操作。

HttpSession 集成

在使用 WebSocket 集成之前,您应该确保首先已经完成了 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 的自定义 SessionConnectEventWebSocketSession 对于在 Spring Session 结束时关闭所有仍然打开的 WebSocket 连接是必需的。

  • SessionRepositoryMessageInterceptor 作为 HandshakeInterceptor 添加到每个 StompWebSocketEndpointRegistration 中。这确保将 Session 添加到 WebSocket 属性中,以便能够更新最后访问时间。

  • SessionRepositoryMessageInterceptor 作为 ChannelInterceptor 添加到我们的入站 ChannelRegistration 中。这确保了每次收到入站消息时,Spring Session 的最后访问时间都会更新。

  • WebSocketRegistryListener 作为 Spring Bean 创建。这确保我们拥有所有 Session ID 到相应 WebSocket 连接的映射。通过维护此映射,我们可以在 Spring Session (HttpSession) 结束时关闭所有 WebSocket 连接。

© . This site is unofficial and not affiliated with VMware.