TsgcWebSocketServer › Methods › WriteData
Sends a message to a single client identified by its connection GUID.
function WriteData(const aGuid, aMessage: string): Boolean;
| Name | Type | Description |
|---|---|---|
aGuid | const string | Identifier of the target connection as assigned by the server when the client handshake completed. |
aMessage | const string | Text payload to deliver to that client as a WebSocket text frame. |
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)
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 (not supported when IOHandlerOptions.IOHandlerType = iohIOCP). Use Broadcast to reach every active client in one call.
oServer.WriteData('guid', 'My First sgcWebSockets Message!.');
function WriteData(const aGuid: String; aStream: TStream; aSize: Integer = 0; const aStreaming: TwsStreaming = stmNone) : Boolean;
| Name | Type | Description |
|---|---|---|
aGuid | const String | Identifier of the target connection as assigned by the server when the client handshake completed. |
aStream | TStream | Source stream whose contents are sent to the client as a WebSocket binary frame. The caller remains the owner of the stream. |
aSize | 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. |
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)
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, 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.
oStream := TMemoryStream.Create;
try
oStream.LoadFromFile('payload.bin');
oServer.WriteData('guid', oStream);
finally
oStream.Free;
end;