Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 864
onkami Avatar asked Nov 07 '22 22:11

onkami


1 Answers

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


}
like image 121
Artem Bilan Avatar answered Nov 15 '22 05:11

Artem Bilan