问题描述
我目前正在使用该类配置Spring Websocket
I am configuring currently my Spring Websocket using the class
public class WebSocketConfig extends WebSocketMessageBrokerConfigurationSupport
现在我遇到了以下建议: Spring STOMP Websockets:通过任何方式在服务器端启用permessage-deflate?
now I came across the advice Spring STOMP Websockets: any way to enable permessage-deflate on server side?
利用
public class SampleJettyWebSocketsApplication implements WebSocketConfigurer
并覆盖
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry)
和报价
@Bean
public DefaultHandshakeHandler handshakeHandler()
问题, WebSocketConfigurer 和 WebSocketMessageBrokerConfigurationSupport 之间是什么关系?换句话说,是否可以通过第一类API WebSocketMessageBrokerConfigurationSupport从WebSocketConfigurer实现中添加配置,以便所有配置都保留在一个文件中?
Question, what is the relation between WebSocketConfigurer and WebSocketMessageBrokerConfigurationSupport? In other words, can I possibly somehow add configuration from WebSocketConfigurer implementation via API of the first class, WebSocketMessageBrokerConfigurationSupport, so all configuration remains in one single file?
推荐答案
WebSocketMessageBrokerConfigurationSupport
实现是通过@EnableWebSocketMessageBroker
配置的DelegatingWebSocketMessageBrokerConfiguration
.您在自定义代码中所需的只是WebSocketMessageBrokerConfigurer
实现.然后将其注入DelegatingWebSocketMessageBrokerConfiguration
:
The WebSocketMessageBrokerConfigurationSupport
implementation is DelegatingWebSocketMessageBrokerConfiguration
which is configured via @EnableWebSocketMessageBroker
. All you need in your custom code is WebSocketMessageBrokerConfigurer
implementation. And that one is injected into DelegatingWebSocketMessageBrokerConfiguration
:
@Autowired(required = false)
public void setConfigurers(List<WebSocketMessageBrokerConfigurer> configurers) {
这是我的测试用例中的一个示例配置:
This is a sample config from my test-cases:
@Configuration
@EnableWebSocketMessageBroker
static class ServerConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Bean
public DefaultHandshakeHandler handshakeHandler() {
return new DefaultHandshakeHandler(new TomcatRequestUpgradeStrategy());
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws")
.setHandshakeHandler(handshakeHandler())
.setAllowedOrigins("http://foo.com")
.addInterceptors(new HandshakeInterceptor() {
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
return request.getHeaders().getOrigin() != null;
}
@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response,
WebSocketHandler wsHandler, Exception exception) {
}
})
.withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry configurer) {
configurer.setApplicationDestinationPrefixes("/app")
.enableSimpleBroker("/topic", "/queue");
}
}
这篇关于Spring Websocket配置:一起使用WebSocketMessageBrokerConfigurationSupport和WebSocketConfigurer-怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!