TsgcWebSocketHTTPServerMethods › WriteData

WriteData Method

Sends a WebSocket 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 socket; False when no matching active connection exists. (Boolean)

Remarks

This overload looks up the WebSocket 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. Only upgraded WebSocket sessions are eligible; GUIDs belonging to pure HTTP requests are rejected and False is returned. 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 (not supported when IOHandlerOptions.IOHandlerType = iohIOCP). 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 socket; False when no matching active connection exists. (Boolean)

Remarks

This overload targets a single WebSocket 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, until the send is complete). 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 (not supported when IOHandlerOptions.IOHandlerType = iohIOCP). Use aSize together with aStreaming when sending very large payloads that should be fragmented across multiple WebSocket frames.

Example


using (var oStream = new MemoryStream())
{
  oStream.LoadFromFile("payload.bin");
  oServer.WriteData("guid", oStream);
}

Back to Methods