dreamworkAPI
Ctrl K
GitHub
classDocs / Message

Message

classsharedmenusource
Object → Message

A named channel that sends data through hidden console commands, so it also works between the menu and the client states, where the net library doesn't.

The server sends to the client state of players, the client sends to the server or the menu, the menu sends to the client. The data is split into url-safe base64 packets of one console line each, paced by a per-frame byte budget, and is delivered to the handlers only when complete.

It is slow ( ~45 KiB/s with a 20 fps client ) and has no checksum, so prefer Network for large data between the server and the client.

Every realm must create the message with the same name to receive it, creating a message with the name of an existing one returns the existing object.

Messages to the same target are queued and sent in order.

Constructor

Message(name: string, can_receive: boolean?): Message

Fields

NameTypeDescription
namestringThe name of the message, read-only.
receiverbooleantrue if the message accepts incoming data, false by default.
timeoutnumberThe number of seconds without a packet after which an incomplete incoming transfer is dropped, 15 by default, at least 1.
limitintegerThe maximum size of an incoming message in bytes, 256 KiB by default, 0 rejects everything.

Methods

abortCancels the outgoing transfers of this message to the target, the packets already sent are not recalled.
attachAttaches a handler that is called when a message is fully received.
awaitWaits for the next fully received message.
clearRemoves every receive handler.
detachDetaches a previously attached receive handler.
isReceivingChecks if the message is receiving data from the sender.
isSendingChecks if the message has queued outgoing packets to the target.
onceAttaches a handler that automatically detaches itself right before being invoked, so it only ever runs once.
sendSends the data to the target.
transmitBuilds the data with a binary writer and sends it to the target.

#Message:abort

sharedmenusource
function Message:abort(target?: ( garrysmod.Player | Message.Realm )?): boolean

Cancels the outgoing transfers of this message to the target, the packets already sent are not recalled.

Parameters
NameTypeDescription
targetoptional( garrysmod.Player | Message.Realm )?The target player on the server, the target realm on the client, ignored in the menu.
Returns
TypeNameDescription
booleancancelledtrue if anything was cancelled.

#Message:attach

sharedmenusource
function Message:attach(handler: Message.ReceiveFn | Hook | Event)

Attaches a handler that is called when a message is fully received.

If the handler is already attached, it will not be attached twice.

Parameters
NameTypeDescription
handlerMessage.ReceiveFn | Hook | EventThe callback or callable object.

#Message:await

sharedmenuasyncsource
async function Message:await(): Message, BinaryReader, garrysmod.Player | nil, Message.Realm

Waits for the next fully received message.

Returns
TypeNameDescription
MessagemessageThe message.
BinaryReaderreaderThe reader of the message.
garrysmod.Player | nilsenderThe sender, nil on the client and menu.
Message.RealmrealmThe realm the message claims to come from.

#Message:clear

sharedmenusource
function Message:clear()

Removes every receive handler.

#Message:detach

sharedmenusource
function Message:detach(handler: Message.ReceiveFn | Hook | Event): boolean

Detaches a previously attached receive handler.

Parameters
NameTypeDescription
handlerMessage.ReceiveFn | Hook | EventThe exact handler reference.
Returns
TypeNameDescription
booleanremovedtrue if the handler was attached.

#Message:isReceiving

sharedmenunodiscardsource
function Message:isReceiving(sender?: ( garrysmod.Player | Message.Realm )?): boolean

Checks if the message is receiving data from the sender.

Parameters
NameTypeDescription
senderoptional( garrysmod.Player | Message.Realm )?The sender player on the server, the sender realm on the client, ignored in the menu.
Returns
TypeNameDescription
booleanis_receivingtrue if an incomplete transfer from the sender is in progress.

#Message:isSending

sharedmenunodiscardsource
function Message:isSending(target?: ( garrysmod.Player | Message.Realm )?): boolean

Checks if the message has queued outgoing packets to the target.

Parameters
NameTypeDescription
targetoptional( garrysmod.Player | Message.Realm )?The target player on the server, the target realm on the client, ignored in the menu.
Returns
TypeNameDescription
booleanis_sendingtrue if a transfer of this message to the target is queued.

#Message:once

sharedmenusource
function Message:once(handler: Message.ReceiveFn | Hook | Event)

Attaches a handler that automatically detaches itself right before being invoked, so it only ever runs once.

Parameters
NameTypeDescription
handlerMessage.ReceiveFn | Hook | EventThe callback or callable object.

#Message:send

sharedmenusource
function Message:send(data: string, target?: ( garrysmod.Player | garrysmod.Player[] | Message.Realm )?, callback?: Message.FinishFn)

Sends the data to the target.

The data is split into console command packets that are paced over frames, the ~8 kb per frame console limit makes it slow ( ~45 kb/s with a 20 fps client ), so prefer Network for large data between the server and the client.

The server sends to the client state of the target players, the client sends to "server" ( default ) or "menu", the menu sends to the client.

Messages to the same target are queued and sent in order.

Parameters
NameTypeDescription
datastringThe data to send.
targetoptional( garrysmod.Player | garrysmod.Player[] | Message.Realm )?The target player(s) on the server, the target realm on the client, ignored in the menu.
callbackoptionalMessage.FinishFnThe callback that is called when the last packet is handed to the engine, or the target left.

#Message:transmit

sharedmenusource
function Message:transmit(fn: fun( message: Message, writer: BinaryWriter ), target?: ( garrysmod.Player | garrysmod.Player[] | Message.Realm )?, callback?: Message.FinishFn)

Builds the data with a binary writer and sends it to the target.

Parameters
NameTypeDescription
fnfun( message: Message, writer: BinaryWriter )The function that writes the data.
targetoptional( garrysmod.Player | garrysmod.Player[] | Message.Realm )?The target player(s) on the server, the target realm on the client, ignored in the menu.
callbackoptionalMessage.FinishFnThe callback that is called when the last packet is handed to the engine, or the target left.
Generated from the dreamwork meta branch.