How to quickly close unresponsive WebSockets in Java spring Tomcat?

I have a real-time application. The client uses WebSockets to connect to the spring framework server running spring boot Tomcat I want the server to quickly (within 5 seconds) detect that the client stops responding due to network disconnection or other problems and close the websocket

I tried

>As described in the document, set "maximum session idle timeout" to "configure 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 don't know if this is implemented correctly because I don't see the link between the servlet server container factory bean and my next generation client handler

>Send Ping messages from the server every 2.5 seconds After manually disconnecting the client and disconnecting the network connection, the server can quickly send Ping for 30 seconds until a transmission error occurs. > 1 and 2 > 1 and 2 at the same time, and in application Set server in properties session-timeout = 5

My test method is:

>Connect websocket from laptop client to Tomcat server > use physical switch to close network connection on laptop > wait for Tomcat server event

How can the spring framework Tomcat server quickly detect that the client is disconnected or does not respond to close the websocket?

Solution

Application events may help you

PS:Annotation driven events

PS2: I made an example project for you

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>