WebSocket Client Before Connect and Before WatchDog

WebSocket Client can be customized with some of the events available. There are 2 new events added to the latest version:

OnBeforeConnect: this event is called just before the client tries to connect to server

OnBeforeWatchDog: sgcWebSocket client has a built-in method to reconnect after a disconnection called WatchDog, this event is called before tries to reconnect.


This event is called before the client tries to connect to server, the event can be raised because the user manually tries to connect to server or because WatchDog is enabled and automatically tries to reconnect after a disconnection.

This method can be used to customize the client properties, example: you can change here the Host / port of the server that is trying to access after too many retries.

procedure OnBeforeConnect(Sender: TObject);
  if Retries > 10 then
    TsgcWebSocketClient(Sender).URL := 'ws://echo.websocket.org';


This event is called when WatchDog is enabled and tries to reconnect to server, here you can use the event to use your own reconnection algorithm. Example: you can try first connect to a secure server and if doesn't work try to reconnect to a non secure server.

procedure OnBeforeWatchDog(Sender: TObject; var Handled:
  Handled := True;

  TsgcWebSocketClient(Sender).URL := 'wss://server1';
  if not TsgcWebSocketClient(Sender).Connect() then
    TsgcWebSocketClient(Sender).URL := 'ws://server2';
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

HTTP/2 Alternate Service
sgcWebSockets 4.4.6

Related Posts