TsgcWebSocketClient › 事件 › OnError

OnError 事件

每次连接上发生 WebSocket 协议错误时触发。

语法

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

默认值

—

备注

OnError 报告 WebSocket 层检测到的协议级问题,例如无效的消息类型、格式错误的帧,或在启用 Options.ValidateUTF8 时的无效 UTF-8 负载。Error 参数包含故障的文字描述;Connection 参数标识受影响的会话。对于未处理的异常(套接字错误、访问违规等),请使用 OnException。当 Connection 已赋值时,组件会在引发 OnError 之后关闭该连接,无论采用何种选项:此时处理程序通常已经返回,但当 NotifyEvents 设置为 neAsynchronous 时,在次要线程上引发的错误会被排队到主线程,其处理程序可能在连接关闭之后才运行。当 Connection 为 nil 时,不会关闭任何内容。

OnError 可能以 Connection = nil 触发,因此处理程序在使用 Connection 之前必须先检查它。WriteData、WriteAndWaitData 和 Ping 会以这种方式报告因客户端未连接而无法发送的消息,WriteData 和 WriteAndWaitData 在 WebSocket 握手尚未完成时也会如此报告。此时 Error 参数为 Data not sent, the client is not connected or its handshake has not completed.(resourcestring S_ERROR_CLIENT_NOT_CONNECTED),事件会在调用该方法的线程上、于方法返回之前触发。客户端重新连接后写入失败时,同样会以 Connection = nil 报告,因此新连接不会被关闭。将 Options.RaiseDisconnectExceptions 设置为 False 可禁用对无法发送的消息的报告。

示例


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;

返回事件