TsgcWebSocketClientMethods › Connect

Connect Method

Opens the WebSocket connection synchronously and blocks the caller until the handshake completes or the timeout elapses.

Syntax

public bool Connect(int aTimeout = 10000);

Parameters

NameTypeDescription
aTimeoutconst IntegerMaximum time in milliseconds to wait for the connection to become active. Defaults to 10000 (10 seconds).

Return Value

True when the client is connected and the WebSocket handshake completed within the timeout; False otherwise. (Boolean)

Remarks

Connect is the blocking counterpart to setting Active := True. It sets Active internally and then waits on an internal event until the connection is established or the timeout expires. If the client is already connected, the method returns True immediately. When the function returns True it is safe to call WriteData straight away, without waiting for OnConnect. Use Start instead if you need a non-blocking call that connects from a secondary thread.

Example


oClient = new TsgcWebSocketClient();
oClient.Host = "127.0.0.1";
oClient.Port = 80;
if (oClient.Connect(5000) == true)
  oClient.WriteData("Hello from client");
else
  ShowMessage("Connection failed");

Back to Methods