Skip to content

Lost connection detection

Marcel Prestel edited this page Feb 27, 2018 · 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

For detection a lost connection we use a heartbeat implementation.

The detection works as following:

Your endpoint (e.g. server) is sending a ping to the connected endpoints (e.g. clients) in a specific interval (default: 60 seconds).

In the next interval the server first checks if he recieved a pong response from the client.

If this is the case, it will send a new ping to the client.

If this is not the case, it will close the connection to the client assuming the connection got lost!

The detection works both ways, so the client is also able to 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();