java spring 服务器关闭,如何在Java Spring Tomcat中快速关闭无响应的websocket?

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?

解决方案

The approach I eventually took was to implement an application-layer ping-pong protocol.

The server sends a ping message with period p to the client.

The client responds to each ping message with a pong message.

If the server sends more than n ping messages without receiving a pong response, it generates a timeout event.

The client can also generate a timeout event if it does not receive a ping message in n*p time.

There should be a much simpler way of implementing this using timeouts in the underlying TCP connection.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值