WebSocket 传输

本节介绍如何配置底层的 WebSocket 服务器传输。

对于 Jakarta WebSocket 服务器,请将 ServletServerContainerFactoryBean 添加到您的配置中。有关示例,请参阅 WebSocket 部分下的 配置服务器

对于 Jetty WebSocket 服务器,请按如下方式自定义 JettyRequestUpgradeStrategy

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

	@Override
	public void registerStompEndpoints(StompEndpointRegistry registry) {
		registry.addEndpoint("/portfolio").setHandshakeHandler(handshakeHandler());
	}

	@Bean
	public DefaultHandshakeHandler handshakeHandler() {
		JettyRequestUpgradeStrategy strategy = new JettyRequestUpgradeStrategy();
		strategy.addWebSocketConfigurer(configurable -> {
				policy.setInputBufferSize(4 * 8192);
				policy.setIdleTimeout(600000);
		});
		return new DefaultHandshakeHandler(strategy);
	}
}

除了 WebSocket 服务器属性外,还有 STOMP WebSocket 传输属性可以按如下方式自定义

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

	@Override
	public void configureWebSocketTransport(WebSocketTransportRegistration registry) {
		registry.setMessageSizeLimit(4 * 8192);
		registry.setTimeToFirstMessage(30000);
	}

}