TsgcWebSocketServer_HTTPAPIMethods › Broadcast

Broadcast Method

Sends the same message to all connected clients, optionally filtered by channel, protocol, or connection GUID list.

Overloads

Overload 1

Syntax

public void Broadcast(string aMessage, string aChannel = '', string aProtocol = '', string Exclude = '', string Include = '');

Parameters

NameTypeDescription
aMessageconst stringText payload delivered to each matching client as a WebSocket text frame.
aChannelconst stringWhen non-empty, the message is sent only to clients subscribed to the given channel.
aProtocolconst stringWhen non-empty, the message is sent only to clients using the given WebSocket sub-protocol.
Excludeconst StringComma-separated list of connection GUIDs that must be skipped by this broadcast.
Includeconst StringComma-separated list of connection GUIDs restricting the broadcast to that subset; ignored when empty.

Remarks

This overload sends a text frame to every active WebSocket connection that matches the supplied filters. Filters are combined with AND semantics: a connection receives the message only when it satisfies every non-empty filter, and is never reached when its GUID appears in Exclude. When Asynchronous is enabled the call returns before each HTTP.sys write completes and the OnAsynchronous event reports completion; when disabled the call runs on the caller's thread and returns once the frames are queued for each socket. Per-connection write errors are reported through OnException without aborting the broadcast.

Example


oServer.Broadcast("Hello From Server");

Overload 2

Syntax

public void Broadcast(TStream aStream, string aChannel = '', string aProtocol = '', string Exclude = '', string Include = '', int aSize = 0, TwsStreaming aStreaming = stmNone);

Parameters

NameTypeDescription
aStreamTStreamSource stream whose contents are delivered to each matching client as a WebSocket binary frame. The caller remains the owner of the stream.
aChannelconst stringWhen non-empty, the message is sent only to clients subscribed to the given channel.
aProtocolconst stringWhen non-empty, the message is sent only to clients using the given WebSocket sub-protocol.
Excludeconst StringComma-separated list of connection GUIDs that must be skipped by this broadcast.
Includeconst StringComma-separated list of connection GUIDs restricting the broadcast to that subset; ignored when empty.
aSizeconst IntegerOptional fragment size in bytes; when greater than zero each client receives the stream split into 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

This overload broadcasts the stream contents as a binary frame to every connection that passes the channel, protocol, Include and Exclude filters. The stream is read from its current position; the server does not take ownership, so the caller must keep the stream alive until Broadcast returns (or, when Asynchronous is enabled, until the OnAsynchronous event reports completion). Use aSize together with aStreaming to stream very large payloads as multiple WebSocket fragments instead of one oversized frame, which is especially useful on HTTP.sys where the kernel buffers the outbound data.

Example


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

Back to Methods