问题
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.
I have a client hosted on a version of Firefox that is willing to negotiate the compression, ie the initial handshake to the WebSocket endpoint includes the compression negotiation header, like:
GET https://my-websocket-host/my-endpoint
...
Sec-WebSocket-Protocol: v10.stomp,v11.stomp
Sec-WebSocket-Extensions: permessage-deflate
Sec-WebSocket-Key: ...
...
, but the server response does not include the permessage-deflate extensions header in the upgrade response, meaning it is not willing to negotiate compression. I have gone on a scavenger hunt for where this could be enabled in configuration but have not found anything. Is there some API I can use to turn this feature on, or is it not supported in the current product?
Thanks very much,
Steve
回答1:
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));
}
来源:https://stackoverflow.com/questions/35347077/spring-stomp-websockets-any-way-to-enable-permessage-deflate-on-server-side