TsgcWebSocketClient › Methods › Connect
Opens the WebSocket connection synchronously and blocks the caller until the handshake completes or the timeout elapses.
function Connect(const aTimeout: Integer = 10000): Boolean; overload;
function Connect(out AError: string; const aTimeout: Integer = 10000): Boolean; overload;
| Name | Type | Description |
|---|---|---|
AError | out string | Returns the description of the connection error when the method fails, or an empty string on success. The same text is available afterwards in LastError. |
aTimeout | const Integer | Maximum time in milliseconds to wait for the connection to become active. Defaults to 10000 (10 seconds). |
True when the client is connected and the WebSocket handshake completed within the timeout; False otherwise. (Boolean)
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, or ConnectTask for a future-style non-blocking connect (Delphi 2010+).
The overload with an out AError parameter behaves identically but additionally returns the reason of a failed attempt, so no event handler is needed to know why the connection did not succeed. LastError is cleared before each attempt and holds the same text afterwards.
oClient := TsgcWebSocketClient.Create(nil);
oClient.Host := '127.0.0.1';
oClient.Port := 80;
if oClient.Connect(5000) then
oClient.WriteData('Hello from client')
else
ShowMessage('Connection failed');