TsgcWebSocketClient › Events › OnError

OnError Event

Fires every time a WebSocket protocol error occurs on the connection.

Syntax

property OnError: TsgcWSErrorEvent;
// TsgcWSErrorEvent = procedure(Connection: TsgcWSConnection; const Error: string) of object

Default Value

—

Remarks

OnError reports protocol-level problems detected by the WebSocket layer, such as an invalid message type, a malformed frame, or an invalid UTF-8 payload when Options.ValidateUTF8 is enabled. The Error parameter contains a textual description of the failure; the Connection parameter identifies the affected session. For unhandled exceptions (socket errors, access violations...) use OnException instead. When Connection is assigned, the component closes that connection once OnError has been raised, whatever the options: the handler has normally returned by then, but with NotifyEvents set to neAsynchronous an error raised on a secondary thread is queued to the main thread and its handler can run after the connection has been closed. When Connection is nil, nothing is closed.

OnError can fire with Connection = nil, so a handler must check Connection before using it. WriteData, WriteAndWaitData and Ping report this way a message that could not be sent because the client is not connected, and WriteData and WriteAndWaitData also when the WebSocket handshake has not completed yet. The Error parameter is then Data not sent, the client is not connected or its handshake has not completed. (resourcestring S_ERROR_CLIENT_NOT_CONNECTED), and the event fires on the thread that called the method, before the method returns. A write that fails after the client has reconnected is reported with Connection = nil as well, so the new connection is not closed. Set Options.RaiseDisconnectExceptions to False to disable the report of messages that could not be sent.

Example


procedure OnError(Connection: TsgcWSConnection; const Error: string);
begin
  if Connection = nil then
    // a write or ping found no connection, nothing was sent
    WriteLn('#not sent: ' + Error)
  else
    WriteLn('#error: ' + Error);
end;

Back to Events