# WebSocket 整合

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

Spring Session 的 WebSocket 支持仅对 Spring 的 WebSocket 支持有效。
具体来说,它不能直接使用JSR-356 (opens new window),因为 JSR-356 没有拦截传入 WebSocket 消息的机制。

# 为什么要使用会话和 WebSockets?

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

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

另一个问题是,根据 JSR-356,如果HttpSession超时,则任何用HttpSession创建的 WebSocket 和经过身份验证的用户都应该被强制关闭。这意味着,如果我们在应用程序中积极地聊天,而不使用 HttpSession,那么我们也会断开与我们的对话的连接。

# WebSocket 用法

WebSocket Sample提供了如何将 Spring Session 与 WebSockets 集成的工作示例。你可以遵循下面几个标题中描述的集成的基本步骤,但我们鼓励你在与自己的应用程序集成时遵循详细的 WebSocket 指南。

# HttpSession积分

在使用 WebSocket 集成之前,你应该确保首先有[HttpSession集成](http-session.html#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。这确保了我们将所有SessionID 映射到相应的 WebSocket 连接。通过维护此映射,我们可以在 Spring Session 结束时关闭所有 WebSocket 连接。