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