本节介绍怎么配置底层的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);
}
}