TsgcTCPClient › 事件 › OnError

OnError 事件

每次 TCP 连接上报告非致命错误时触发。

语法

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

默认值

—

备注

OnError 报告在 TCP 套接字上发送或接收数据时检测到的传输级问题,例如 TLS 握手失败、心跳超时或在调度接收到的有效载荷时引发的错误。Error 参数包含故障的文字描述;Connection 参数标识受影响的会话。对于未处理的异常(套接字错误、访问冲突……),请改用 OnException。当 Connection 已赋值时,组件会在引发 OnError 之后关闭该连接,无论采用何种选项:此时处理程序通常已经返回,但当 NotifyEvents 设置为 neAsynchronous 时,在次要线程上引发的错误会被排队到主线程,其处理程序可能在连接关闭之后才运行。当 Connection 为 nil 时,不会关闭任何内容。

OnError 可能以 Connection = nil 触发,因此处理程序在使用 Connection 之前必须先检查它。WriteData 和 WriteAndWaitData 会以这种方式报告因客户端未连接而无法发送的消息。此时 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
  WriteLn('#error: ' + Error);
end;

返回事件