5

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?

Community
  • 1
  • 1
onkami
  • 8,791
  • 17
  • 90
  • 176
  • Can you tell us why `@EnableWebSocketMessageBroker` isn't enough for you? – Artem Bilan Apr 26 '17 at 17:25
  • @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. – onkami Apr 26 '17 at 17:26

1 Answers1

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


}
Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Thank you Artem, so to add permessage-compress from http://stackoverflow.com/questions/35347077/spring-stomp-websockets-any-way-to-enable-permessage-deflate-on-server-side I shall just borrow handshakeHandler from there, or there is a better way to set up "permessage-compress"? – onkami Apr 26 '17 at 17:33
  • 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. – Artem Bilan Apr 26 '17 at 17:36
  • Thanks a lot, really appreciate your help! :) – onkami Apr 26 '17 at 17:37