Skip to content

Your own messages

For a chat line, a ping or your own HUD, you send bytes with a channel number: from a player to the host, or from the host to players.

Signature What it does Line
bool MultiplayerSession.SendToHost(ushort channel, byte[] data) Client: sends a message to the host. False when it cannot be sent. 366
bool MultiplayerSession.SendTo(int seat, ushort channel, byte[] data) Host: sends to one seat. False for an unknown seat. 369
int MultiplayerSession.Broadcast(ushort channel, byte[] data) Host: sends to every seat in the game and returns how many it went to. 372
event Action<CustomMessage> MultiplayerSession.onCustomMessage Raised on the host for each client message, and on a client for each host message. 363
string PlayerText.Clean(string text, int maxLetters = 32) Makes text a player typed safe to show. PlayerText.cs

A CustomMessage has Seat, Channel and Data. On the host, Seat is the sender’s seat. On a client it is 0, the host. The channel number is yours to define. Messages arrive in the order they were sent.

Limit Value Source
Client to host, one message 1,024 bytes CommandCodec.cs, line 23
Host to a client, one message 64 KB CommandCodec.cs, line 26
Messages one player may send 20 a second, 40 at once above the rate SessionSettings.cs, lines 429 and 432

A message over its size limit is not sent: SendToHost and SendTo return false, Broadcast returns 0. A message over the rate is dropped and counted on the host. The two rates are HostSettings.CustomPerSecond and CustomBurst. Going over this rate only drops the message. A player who goes over the general MessagesPerSecond (30, burst 60), or sends messages an honest game never sends, commits offences; past MaxDrops (200), with DisconnectOffenders on (the default), the host disconnects them.

A typed text can carry line breaks, control characters and marks that turn the writing direction, which can make a text read as another. PlayerText.Clean removes them, trims the ends and cuts the text to maxLetters letters as a reader counts them, so an accented letter or an emoji is never cut in half. Clean what a player typed before you show it on another machine.

Copied from TwoPlayerGame.cs in the two-player sample, lines 51 and 61. Not compiled for this page.

nameSent = MultiplayerSession.SendToHost(NameChannel, Encoding.UTF8.GetBytes(MyName));
...
var name = PlayerText.Clean(Encoding.UTF8.GetString(message.Data ?? new byte[0]), 24);

The host cleans the name to 24 letters, then tells every client all names with Broadcast.

A seat that waits without a character may still send these messages. They carry only its seat number, so check the seat in your own code. For the seats, see Seats, joining and waiting.