Spring Websocket Configuration: using WebSocketMessageBrokerConfigurationSupport and WebSocketConfigurer together - how?

只愿长相守 提交于 2019-12-23 19:22:14

问题


I am configuring currently my Spring Websocket using the class

public class WebSocketConfig extends WebSocketMessageBrokerConfigurationSupport

now I came across the advice Spring STOMP Websockets: any way to enable permessage-deflate on server side?

that makes use of

public class SampleJettyWebSocketsApplication implements WebSocketConfigurer 

and overrides

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry)

and offers

@Bean
public DefaultHandshakeHandler handshakeHandler() 

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?


回答1:


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");
    }


}


来源:https://stackoverflow.com/questions/43637864/spring-websocket-configuration-using-websocketmessagebrokerconfigurationsupport

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!