Once your client has connected to a server, sometimes the connection can be closed due to poor signal, connection errors, etc. There are 2 properties that help keep the connection active.
HeartBeat property allows you to send a Ping every X seconds to keep the connection alive. Some servers close TCP connections if there is no data exchanged between peers. HeartBeat solves this problem by sending a ping at a specific interval. Usually this is enough to maintain a connection active, but you can set a TimeOut interval if you want to close the connection when a response from the 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 customizing HeartBeat behavior. By default, if HeartBeat is enabled, the client will send a WebSocket ping every X seconds as set by the 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.
If WatchDog is enabled, when the client detects a disconnection, WatchDog tries to reconnect every X seconds until the 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 attempt to connect. Example: after 3 retries, if the client cannot connect to a server, it will attempt to connect to a secondary server.
The Handled property, if set to True, means that the client won't try to reconnect.