TsgcWebSocketServer_HTTPAPIMethods › WriteData

WriteData Method

Sends a message to a single client identified by its connection GUID.

Overloads

Overload 1

Syntax

public bool WriteData(string aGUID, string aMessage);

Parameters

NameTypeDescription
aGUIDconst stringIdentifier of the target connection as assigned by the server when the client handshake completed.
aMessageconst stringText payload to deliver to that client as a WebSocket text frame.

Return Value

True when a connection with the given GUID was located and the frame was handed to its HTTP.sys socket; False when no matching active connection exists. (Boolean)

Remarks

This overload looks up the connection whose GUID matches aGUID and sends the text message to that single client. The GUID is the value exposed by TsgcWSConnection.Guid, typically captured in OnConnect and stored by the application. When QueueOptions.Text is set to a value other than qmNone the frame is queued and dispatched from the connection thread rather than the caller's thread, which prevents locks when several threads send to the same client. When Asynchronous is enabled the method returns before HTTP.sys has finished writing the frame; completion is reported through OnAsynchronous. Use Broadcast to reach every active client in one call.

Example


oServer.WriteData("guid", "My First sgcWebSockets Message!.");

Overload 2

Syntax

public bool WriteData(string aGUID, TStream aStream, int aSize = 0, TwsStreaming aStreaming = stmNone);

Parameters

NameTypeDescription
aGUIDconst StringIdentifier of the target connection as assigned by the server when the client handshake completed.
aStreamTStreamSource stream whose contents are sent to the client as a WebSocket binary frame. The caller remains the owner of the stream.
aSizeIntegerOptional 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.

Return Value

True when a connection with the given GUID was located and the stream was handed to its HTTP.sys socket; False when no matching active connection exists. (Boolean)

Remarks

This overload targets a single client with a binary payload read from the supplied stream. The stream is consumed from its current position and the server does not take ownership, so the caller must keep it alive until WriteData returns (or, when queuing or when Asynchronous is enabled, until the send has actually completed). When QueueOptions.Binary is set to a value other than qmNone the frame is queued and dispatched from the connection thread rather than the caller's thread. Use aSize together with aStreaming when sending very large payloads that should be fragmented across multiple WebSocket frames rather than buffered as one oversized message in HTTP.sys.

Example


TMemoryStream oStream = new TMemoryStream();
try {
  oStream.LoadFromFile("payload.bin");
  oServer.WriteData("guid", oStream);
} finally {
  oStream.Free();
}

Back to Methods