-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Support configuring Jetty WebSocket server parameters #30344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Good point that we need to update the documentation there. From what I can see in this example, it involves a ServletContext initialization hook. However, I'm also wondering if you've tried this in Server Configuration that now probably also works for Jetty. |
Yes I have tried to define a |
Actually I meant have you tried what's in the example I referenced under jetty-project? |
Not sure how to apply that in a Spring Boot environment... sorry. |
I have the same problem. The example referenced by @rstoyanchev didn't help. Also when trying to use the Server Configuration, first I had to add jakarta.websocket-api 2.0.0 to classpath, because it was looking for class jakarta.websocket.WebSocketContainer that it didn't find (ClassNotFoundException). Then I got "Attribute 'jakarta.websocket.server.ServerContainer' not found in ServletContext", which -- I suppose -- means that no, this approach doesn't work on Jetty. |
For anyone finding this issue. After pouring through spring code I ended up with 2 approaches for setting the configuration for Jetty. Approach 1: @Bean
public JettyWebSocketServletWebServerCustomizer websocketServletWebServerCustomizer() {
return new JettyCustomizer();
}
public class JettyCustomizer extends JettyWebSocketServletWebServerCustomizer {
public JettyCustomizer() {
}
@Override
public void customize(JettyServletWebServerFactory factory) {
super.customize(factory);
factory.addConfigurations(new AbstractConfiguration() {
@Override
public void configure(WebAppContext context) throws Exception {
JettyWebSocketServerContainer container =
JettyWebSocketServerContainer.getContainer(context.getServletContext());
if (container != null) {
container.setInputBufferSize(textMessageMaxSize);
container.setMaxTextMessageSize(textMessageMaxSize);
}
}
});
}
} This will override the customizer provided by Approach 2 using a handshake handler: @Bean
public DefaultHandshakeHandler handshakeHandler() {
return new DefaultHandshakeHandler(new ServletContextAwareJettyRequestUpgradeStrategy());
}
public class ServletContextAwareJettyRequestUpgradeStrategy extends JettyRequestUpgradeStrategy
implements ServletContextAware {
private ServletContext servletContext;
public ServletContextAwareJettyRequestUpgradeStrategy() {
super();
}
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
@Override
public void upgrade(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol,
List<WebSocketExtension> selectedExtensions, @Nullable Principal user,
org.springframework.web.socket.WebSocketHandler handler, Map<String, Object> attributes)
throws HandshakeFailureException {
if (servletContext != null) {
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
if (container != null) {
container.setInputBufferSize(textMessageMaxSize);
container.setMaxTextMessageSize(textMessageMaxSize);
}
}
super.upgrade(request, response, selectedProtocol, selectedExtensions, user, handler, attributes);
}
} I would love to hear somebody from Spring devs commenting whether there is more elegant way of setting the configuration. Note: Ideally my expectation was that Spring provides a way for devs to provide a bean implementation of |
Thanks for the feedback @djivko. We can probably expose a
|
I have confirmed that Unfortunately this cannot be done in 6.0.x since the |
For WebFlux we can't use ServletContextAware. See gh-30344
Affects: 6.0.7
It is not clear how to configure a WebSocket server using Jetty and Spring Boot 3 / Spring framework 6. More specifically, I want to configure stuff like
idleTimeout
.The doc explained it well for Spring Boot 2 / Spring framework 5, but that doc is obsolete now that
WebSocketPolicy
can't be instantiated anymore,WebSocketServerFactory
doesn't exist anymore, andJettyRequestUpgradeStrategy
has been refactored with only a no-arg constructor. So it seems like that doc needs an update.The text was updated successfully, but these errors were encountered: