When first working with the HTTPAPI Server, it is very common to see that the OnDisconnect event is not fired immediately when a client closes the connection. The reason is that HTTPAPI Server works a bit differently than other servers like Indy. In the Indy server there is a thread for every connection and this thread checks every x milliseconds whether the connection is active. The HTTPAPI Server uses a thread-pool that handles all connections and does not check for every connection whether it is active or not.
In order to get notified when client closes connection, do the following configuration:
1. If you use a TsgcWebSocketClient, set Options.CleanDisconnect := True. This means that before the connection is closed, the client will try to send a notification to the server that the connection will be closed. If the server receives this message, the OnDisconnect event will be called.
2. For other disconnections, the only solution is to write something to the socket; if it fails, the connection is disconnected. Enable HeartBeat on HTTPAPI server, and set an interval of 60 seconds for example and a timeout of 0. This configuration means that every 60 seconds all connections will be pinged, and if any are disconnected, OnDisconnect event will be fired. You can put a lower value of HeartBeat.Interval, but do not set it too low (1 second for example is too low) because the performance of the server will be affected.