I have a real-time application with clients using websockets to connect with a Spring Framework server, which is running Spring Boot Tomcat. I want the server to quickly (within 5 seconds) detect when a client stops responding due to a network disconnect or other issue and close the websocket.
I have tried
Setting the max session idle timeout as described in the documentation as "Configuring the WebSocket Engine" http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html
@Bean public WebSocketHandler clientHandler() { return new PerConnectionWebSocketHandler(ClientHandler.class); } @Bean public ServletServerContainerFactoryBean createWebSocketContainer() { ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean(); container.setMaxSessionIdleTimeout(5000); container.setAsyncSendTimeout(5000); return container; }
I am not sure this is implemented correctly because I do not see the link between the ServletServerContainerFactoryBean and my generation of ClientHandlers.
Sending ping messages from server every 2.5 seconds. After I manually disconnect the client by breaking the network connection, the server happily sends pings for another 30+ seconds until a transport error appears.
1 and 2 simultaneously
1 and 2 and setting
server.session-timeout = 5
in application.properties
My methodology for testing this is to:
- Connect a websocket from a laptop client to the Tomcat server
- Turn off network connection on the laptop using the physical switch
- Wait for Tomcat server events
How does a Spring FrameworkTomcat server quickly detect that a client has been disconnected or not responding to close the websocket?