Spring STOMP Websockets: any way to enable permessage-deflate on server side?

后端 未结 1 456
清歌不尽
清歌不尽 2021-01-06 14:48

I\'m working with spring-websockets under the spring-boot-starter 1.3.1.RELEASE, with the Jetty backend. I am wondering how to enable permessage-deflate in the server.

1条回答
  •  别那么骄傲
    2021-01-06 15:42

    This feature needs to be enabled in Jetty itself, as I believe this extension is not registered by default. Spring provides a way to configure the handshake handler and enable that extension.

    The principle is explained in the reference documentation on websocket server configuration, but here's a complete example:

    @Configuration
    @EnableWebSocket
    public class SampleJettyWebSocketsApplication implements WebSocketConfigurer {
    
        @Override
        public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
            // make sure to use the handshake handler we've defined
            registry.addHandler(echoWebSocketHandler(), "/echo")
                    .setHandshakeHandler(handshakeHandler()).withSockJS();
        }
    
        @Bean
        public DefaultHandshakeHandler handshakeHandler() {
    
            WebSocketServerFactory factory = new WebSocketServerFactory();
            // add the "permessage-compress" Websocket extension
            factory.getExtensionFactory()
                   .register("permessage-compress", PerMessageDeflateExtension.class);
            return new DefaultHandshakeHandler(new JettyRequestUpgradeStrategy(factory));
        }
    

    0 讨论(0)
提交回复
热议问题