TsgcWebSocketClient |Client Keep Connection Open

Once your client has connected to server, sometimes connection can be closed due to poor signal, connection errors... there are 2 properties which helps to keep connection active.

 

HeartBeat

HeartBeat property allows to send a Ping every X seconds to maintain connection alive. Some servers, close TCP connections if there is no data exchanged between peers. HeartBeat solves this problem, sending a ping every a specific interval. Usually this is enough to maintain a connection active, but you can set a TimeOut interval if you want to close connection if a response from server is not received after X seconds.

 

Example: send a ping every 30 seconds


oClient := TsgcWebSocketClient.Create(nil);
oClient.HeartBeat.Interval := 30;
oClient.HeartBeat.Timeout := 0;
oClient.HeartBeat.Enabled := true;
oClient.Active := true;

There is an event called OnBeforeHeartBeat which allows to customize HeartBeat behaviour. By default, if HeartBeat is enabled, client will send a websocket ping every X seconds set by HeartBeat.Interval property.

OnBeforeHeartBeat has a parameter called Handled, by default is false, which means the flow is controlled by TsgcWebSocketClient component. If you set the value to True, then ping won't be sent, and you can send your custom message using Connection class.

 

WatchDog

If WatchDog is enabled, when client detects a disconnection, WatchDog try to reconnect again every X seconds until connection is active again.

 

Example: reconnect every 10 seconds after a disconnection with unlimited attempts.

 


oClient := TsgcWebSocketClient.Create(nil);
oClient.WatchDog.Interval := 10;
oClient.WatchDog.Attempts := 0;
oClient.WatchDog.Enabled := true;
oClient.Active := true;

You can use OnBeforeWatchDog event to change the Server where the client will try to connect. Example: after 3 retries, if the client cannot connect to a server, will try to connect to a secondary server.

The Handled property, if set to True, means that the client won't try to reconnect.