TsgcWebSocketClient › Methods › WriteAndWaitData
Sends a text message and blocks the caller until the server responds with a text message or the timeout elapses.
public string WriteAndWaitData(string aText, int aTimeout = 10000);
| Name | Type | Description |
|---|---|---|
aText | const String | The text payload to send to the server as a WebSocket text frame. |
aTimeout | const Integer | Maximum time in milliseconds to wait for the server reply. Defaults to 10000 (10 seconds). |
The text message received from the server, or an empty string when the timeout expired, no connection was available, or an error occurred. (string)
WriteAndWaitData is a convenience helper for request/response style exchanges over a WebSocket. It sends the text frame and then blocks the calling thread on an internal event until the next text message arrives from the server or the timeout fires. Because it blocks, it should not be called from the main UI thread unless a short timeout is used. An empty string is returned when the client is not connected or when the server does not reply in time; exceptions at the socket level are trapped and forwarded to OnError. Use the stream overload when you need to send and receive binary payloads.
string vReply = oClient.WriteAndWaitData("ping", 5000);
if (vReply != "")
ShowMessage(vReply);
public TStream WriteAndWaitData(TStream aStream, int aSize = 0, TwsStreaming aStreaming = stmNone, int aTimeout = 10000);
| Name | Type | Description |
|---|---|---|
aStream | const TStream | Source stream whose contents will be sent as a WebSocket binary frame. The caller remains the owner of the stream. |
aSize | const Integer | Optional fragment size in bytes; when greater than zero the outgoing stream is split into multiple fragments of this size. Use 0 (the default) to send the whole stream as a single frame. |
aStreaming | const TwsStreaming | Streaming mode for fragmented transmission (stmNone, stmStart, stmContinue, stmFinish). Defaults to stmNone, which sends a complete standalone message. |
aTimeout | const Integer | Maximum time in milliseconds to wait for the server reply. Defaults to 10000 (10 seconds). |
A newly allocated stream containing the binary message received from the server, or nil when the timeout expired, no connection was available, or an error occurred. The caller is responsible for freeing the returned stream. (TStream)
This overload is the binary counterpart to the text the other overload. It writes the supplied stream as a binary frame and then blocks the calling thread on an internal event until the next binary message arrives from the server or the timeout fires. Because it blocks, avoid calling it from the main UI thread with a long timeout. The return value is nil when the client is not connected or the server does not reply in time; exceptions are trapped and forwarded to OnError. Free both the source stream (after the call) and the returned stream when you are done with them.
byte[] request = File.ReadAllBytes("request.bin");
TStream oReply = oClient.WriteAndWaitData(request, 0, TwsStreaming.stmNone, 5000);
if (oReply != null)
{
// consume oReply, then dispose it
oReply.Free();
}