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;
| Name | Type | Description |
|---|---|---|
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.
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');