TsgcWebSocketClient › Methods › WriteData
Sends a text message to the WebSocket server, optionally splitting it into fragments of a given size.
public void WriteData(string aText, int aSize = 0, TwsStreaming aStreaming = stmNone);
| Name | Type | Description |
|---|---|---|
aText | const String | The text payload to send to the server as a WebSocket text frame. |
aSize | const Integer | Optional 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. |
aStreaming | const TwsStreaming | Streaming mode for fragmented transmission (stmNone, stmStart, stmContinue, stmFinish). Defaults to stmNone, which sends a complete standalone message. |
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.
oClient.WriteData("My First sgcWebSockets Message!.");
public void WriteData(TStream aStream, int aSize = 0, TwsStreaming aStreaming = stmNone);
| 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 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. |
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).
byte[] bytes = File.ReadAllBytes("payload.bin");
oClient.WriteData(bytes);