TsgcWebSocketClient › Methods › ConnectTask
Opens the WebSocket connection in a background task and returns an IsgcTask handle that can be awaited, chained or cancelled.
function ConnectTask(const aTimeout: Integer = 10000): IsgcTask;
| Name | Type | Description |
|---|---|---|
aTimeout | const Integer | Maximum time in milliseconds to wait for the connection to become active. Defaults to 10000 (10 seconds). |
An IsgcTask handle for the pending connection attempt. Call Await to block until it finishes, ThenProc to run a continuation on success, OnError to handle failures, and Cancel to abort. IsCompleted, IsCancelled and State report progress.
ConnectTask is the future-style counterpart of Connect: it runs the blocking connect on a background thread and returns immediately, so the calling thread is never blocked. If the connection cannot be established within aTimeout, the task fails with EsgcAsyncException and the OnError continuation runs. Calling Cancel on the returned task disconnects the client, so a connection attempt still in progress is aborted instead of running to completion. Requires Delphi 2010 or later (and C++Builder 2010 or later); it is not available in the .NET edition.
oClient := TsgcWebSocketClient.Create(nil);
oClient.URL := 'wss://www.esegece.com:2053';
oClient.ConnectTask(5000)
.ThenProc(procedure
begin
oClient.WriteData('Hello from client');
end)
.OnError(procedure(E: Exception)
begin
ShowMessage('Connection failed: ' + E.Message);
end);