TsgcWebSocketClientMethods › WriteAndWaitData

WriteAndWaitData Method

Sends a text message and blocks the caller until the server responds with a text message or the timeout elapses.

Overloads

Overload 1

Syntax

public string WriteAndWaitData(string aText, int aTimeout = 10000);

Parameters

NameTypeDescription
aTextconst StringThe text payload to send to the server as a WebSocket text frame.
aTimeoutconst IntegerMaximum time in milliseconds to wait for the server reply. Defaults to 10000 (10 seconds).

Return Value

The text message received from the server, or an empty string when the timeout expired, no connection was available, or an error occurred. (string)

Remarks

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.

Example


string vReply = oClient.WriteAndWaitData("ping", 5000);
if (vReply != "")
  ShowMessage(vReply);

Overload 2

Syntax

public TStream WriteAndWaitData(TStream aStream, int aSize = 0, TwsStreaming aStreaming = stmNone, int aTimeout = 10000);

Parameters

NameTypeDescription
aStreamconst TStreamSource stream whose contents will be sent as a WebSocket binary frame. The caller remains the owner of the stream.
aSizeconst IntegerOptional 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.
aStreamingconst TwsStreamingStreaming mode for fragmented transmission (stmNone, stmStart, stmContinue, stmFinish). Defaults to stmNone, which sends a complete standalone message.
aTimeoutconst IntegerMaximum time in milliseconds to wait for the server reply. Defaults to 10000 (10 seconds).

Return Value

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)

Remarks

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.

Example


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();
}

Back to Methods