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

1

6

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?

Underpainting answered 26/4, 2017 at 15:0 Comment(2)
Can you tell us why @EnableWebSocketMessageBroker isn't enough for you?Assured
@ArtemBilan, I am just a bit weak in the whole plethora of your objects :)) What I need is to enable permessage-deflate (see link in the question). I also override OutgoingExecutorChannel because we need strict sequencing in multithreading send.Underpainting
A
2

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


}
Assured answered 26/4, 2017 at 17:30 Comment(3)
Thank you Artem, so to add permessage-compress from #35347577 I shall just borrow handshakeHandler from there, or there is a better way to set up "permessage-compress"?Underpainting
I'm not familiar with that feature. But since Brian has answered there to proceed via getExtensionFactory(), I think there is really no other way.Assured
Thanks a lot, really appreciate your help! :)Underpainting

© 2022 - 2024 — McMap. All rights reserved.