TsgcWebSocketClientMethods › Ping

Ping Method

Sends a WebSocket ping frame to the server and returns immediately without waiting for the pong response.

Overloads

Overload 1

Syntax

public void Ping(string aText = '');

Parameters

NameTypeDescription
aTextconst StringOptional payload to carry inside the ping frame; pass an empty string to send a ping with no payload.

Remarks

This overload fires the ping and returns straight away, without blocking the caller or waiting for the matching pong. It is a no-op when the client is not connected. Any pong received from the server is delivered asynchronously through the standard connection events. Internally exceptions are trapped and routed to OnError. Use the timeout overload of the other overload when you need confirmation that the peer is still reachable.

Example


oClient.Ping("keepalive");

Overload 2

Syntax

public bool Ping(int aTimeout, string aText = '');

Parameters

NameTypeDescription
aTimeoutIntegerMaximum time in milliseconds to wait for the pong reply before giving up.
aTextconst StringOptional payload to carry inside the ping frame; pass an empty string to send a ping with no payload.

Return Value

True if the server replied with a matching pong inside the timeout window; False otherwise. When False is returned the client may choose to close the connection because the peer is considered unreachable. (Boolean)

Remarks

This overload blocks the calling thread until either the pong arrives or the timeout elapses, so it is useful for interactive liveness checks. The method returns False immediately when the client is not connected. Exceptions raised by the underlying socket are caught and forwarded to OnError. For a fire-and-forget ping that does not block, use the parameterless the other overload overload instead.

Example


if (oClient.Ping(5000, "healthcheck") == false)
  oClient.Disconnect();

Back to Methods