TsgcWebSocketClientMethods › WriteData

WriteData Method

Sends a text message to the WebSocket server, optionally splitting it into fragments of a given size.

Overloads

Overload 1

Syntax

public void WriteData(string aText, int aSize = 0, TwsStreaming aStreaming = stmNone);

Parameters

NameTypeDescription
aTextconst StringThe text payload to send to the server as a WebSocket text frame.
aSizeconst IntegerOptional fragment size in bytes; when greater than zero the message is split into multiple fragments of this size. Use 0 (the default) to send the whole message as a single frame.
aStreamingconst TwsStreamingStreaming mode for fragmented transmission (stmNone, stmStart, stmContinue, stmFinish). Defaults to stmNone, which sends a complete standalone message.

Remarks

WriteData transmits the string as a WebSocket text frame on the active connection. By default the call runs on the calling thread and returns once the bytes are handed to the socket. When QueueOptions.Text is set to a value other than qmNone, the message is queued and actually sent from the connection's own thread, which prevents locks when several threads write concurrently. The call is a no-op when the client is not connected, and any socket exception is forwarded to OnError. Use the stream overload when sending large payloads or binary data.

Example


oClient.WriteData("My First sgcWebSockets Message!.");

Overload 2

Syntax

public void WriteData(TStream aStream, int aSize = 0, TwsStreaming aStreaming = stmNone);

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 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.

Remarks

WriteData reads the supplied stream from its current position and transmits the contents as a WebSocket binary frame. The call runs on the calling thread by default; when QueueOptions.Binary is set to a value other than qmNone, the message is queued and actually dispatched from the connection thread. The method is a no-op when the client is not connected, and socket exceptions are captured and forwarded to OnError. Free the stream only after the call returns (or, when queuing, after the send is complete).

Example


byte[] bytes = File.ReadAllBytes("payload.bin");
oClient.WriteData(bytes);

Back to Methods