Skip to content

Lost connection detection

Noah Andrews edited this page Apr 17, 2019 · 7 revisions

Introduction

Lost connection detection (I know Germans come up with the best names!) is a feature to detect if a connection to a another endpoint is lost e.g. due to losing wifi or mobile data signal.

How does it work

To detect lost connections, we use a heartbeat implementation.

The detection runs at a specified interval (default: 60 seconds), and performs the following actions for all connected endpoints:

  • Disconnects the endpoint if it has not sent a pong recently enough. Endpoints are given 1.5x the interval to reply with a pong. So if the interval is 60 seconds, endpoints are given 90 seconds to respond.
  • Sends a ping to the endpoint

The detection works both ways, so the server can detect lost clients, and the client can detect a lost connection to the server.

This feature takes advantage of the specification in section 5.5.2 that a endpoint SHOULD respond to a Ping frame with Pong frame as soon as is practical.

Example

The method to change the interval is setConnectionLostTimeout(), both for the server and the client.

Change the check interval

To change the check interval, simple use the method setConnectionLostTimeout() and set your check interval in seconds

For example if you want to change the interval to 30 seconds use the following code.

ChatServer s = new ChatServer( port );
s.setConnectionLostTimeout( 30 );
s.start();

Disable lost connection detection

A value lower or equal 0 results in the check to be deactivated.

ChatServer s = new ChatServer( port );
s.setConnectionLostTimeout( 0 );
s.start();