6

I have an application in which clients use websockets to connect to a server which is running Spring Boot Tomcat. My question is if there is a way for the server to detect a client disconnect due to a network loss.

Thanks.

R. Potra
  • 71
  • 1
  • 5

2 Answers2

0

if you are using stomp , check SessionDisconnectEvent. For raw Websocket connections, you can use : WebSocketHandler-->afterConnectionClosed

melihcoskun
  • 326
  • 1
  • 4
  • 19
  • I'm not using stomp. The afterConnectionClosed method from WebSocketHandler doesn't get triggered when a client looses it's internet connection. – R. Potra Jan 30 '17 at 06:51
  • did you check -> handleTransportError method ? – melihcoskun Jan 30 '17 at 11:51
  • handleTransportError should be triggered but you should identify the exception . if it is a websocket disconnection type of exception, you can fire your event.. – melihcoskun Jan 30 '17 at 12:01
  • handleTransportError gets triggered only after sending some messages through the websocket to the disconnected client and only after 6-7 minutes, so the messages sent get lost. Do you have any idea of why handleTransportError gets trigger with such delay after writing to the websocket? – R. Potra Jan 31 '17 at 13:28
  • handleTransportError is being caled when all kind of websocket connection problems occured, not waiting anything. 6-7 minute is too long, are you set ServletServerContainerFactoryBean.setMaxSessionIdleTimeout ? By the way, Tomcat or your proxy can close the websocket session if the tcp connection has been idle for more than Tomcat or Proxy Idle seconds.You need both ping-pong mechanism and websocket connection lost controls. – melihcoskun Jan 31 '17 at 14:45
  • I'm not setting ServletServerContainerFactoryBean.setMaxSessionIdleTimeout. Do you know how Tomcat idle seconds could be set? – R. Potra Feb 01 '17 at 15:47
  • i guess it is : 30 – melihcoskun Feb 02 '17 at 15:25
  • I've added 'server.session.timeout' property in application.properties file and set it to value 30 but it doesn't seem to have any effect. – R. Potra Feb 08 '17 at 12:39
0

I have searched before for this and the solution I was able to find was to implement a ping-pong mechanism between the server and the clients.

For example, each few seconds send a dummy message to the client on a specific topic and receive back another dummy reply, if you didn't get a reply for a configured period you can consider the client disconnected.

As mentioned here,

STOMP and Spring also allow us to set up topics, where every subscriber will receive the same message. This is going to be very useful for tracking active users. In the UI, each user subscribes to a topic that reports back which users are active, and in our example that topic will produce a message every 2 seconds. The client will reply to every message containing a list of users with its own heartbeat, which then updates the message being sent to other clients. If a client hasn't checked in for more than 5 seconds (i.e. missed two heartbeats), we consider them offline. This gives us near real time resolution of users being available to chat. Users will appear in a box on the left hand side of the screen, clicking on a name will pull up a chat window for them, and names with an envelope next to them have new messages.

Amr K. Ismail
  • 368
  • 2
  • 9