gpt4 book ai didi

Spring Websocket 配置 : using WebSocketMessageBrokerConfigurationSupport and WebSocketConfigurer together - how?

转载 作者:行者123 更新时间:2023-12-01 10:59:17 25 4
gpt4 key购买 nike

我目前正在使用该类配置我的 Spring Websocket

public class WebSocketConfig extends WebSocketMessageBrokerConfigurationSupport

现在我遇到了建议 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()

问题, 之间的关系是什么? WebSocket配置器 WebSocketMessageBrokerConfigurationSupport ?换句话说,我是否可以通过第一类的 API WebSocketMessageBrokerConfigurationSupport 以某种方式从 WebSocketConfigurer 实现添加配置,因此所有配置都保留在一个文件中?

最佳答案

WebSocketMessageBrokerConfigurationSupport实现是 DelegatingWebSocketMessageBrokerConfiguration通过 @EnableWebSocketMessageBroker 配置.您在自定义代码中只需要 WebSocketMessageBrokerConfigurer执行。而那个注入(inject)到 DelegatingWebSocketMessageBrokerConfiguration :

@Autowired(required = false)
public void setConfigurers(List<WebSocketMessageBrokerConfigurer> configurers) {

这是我的测试用例中的示例配置:
@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 配置 : using WebSocketMessageBrokerConfigurationSupport and WebSocketConfigurer together - how?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43637864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com