TsgcWebSocketServer | Server Keep Connections Alive

Once a client has connected to the server, sometimes the connection can be closed due to poor signal, connection errors, etc. Use HeartBeat to keep the connection alive.

 

HeartBeat

The HeartBeat property allows you to send a Ping every X seconds to maintain the connection alive. Some clients 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 client is not received after X seconds.

 

Example: send a ping to all connected clients every 30 seconds


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