D
D
DOTSNET
Search…
NetworkMessage

Network Message Protocol

DOTSNET comes with its own message protocol. While you will never have to worry about it, it is good to know when debugging or designing external applications.
    Client->Server Messages: <<messageId:ushort, message:bytes>>
    Server->Client Messages: <<messageId:ushort, amount:uint, messages:bytes>>

NetworkMessage Interface

All messages need to be structs that implement the NetworkMessage interface.
Function:
Description:
Serialize(IBitWriter)
Serializes message contents into writer.
Deserialize(IBitReader)
Deserializes message contents from reader.

Sending a NetworkMessage

NetworkServerSystem.Send can send a NetworkMessage to a client connection.
NetworkClientSystem.Send can send a NetworkMessage to the server.
Whenever we want to send something from the client to the server, or from the server to a client, we need to use a NetworkMessage.
Simply create a struct that implements the NetworkMessage interface and add the serialize & deserialize methods:
1
using DOTSNET;
2
3
public struct JoinWorldMessage : NetworkMessage
4
{
5
public Bytes16 playerPrefabId;
6
7
public bool Serialize(ref IBitWriter writer) =>
8
writer.WriteBytes16(playerPrefabId);
9
10
public bool Deserialize(ref IBitReader reader) =>
11
reader.ReadBytes16(out playerPrefabId);
12
}
Copied!
Message IDs are automatically generated from the message's type name hash.

Receiving a NetworkMessage

Inherit from NetworkServerMessageSystem to handle a received message on the server:
1
using DOTSNET;
2
3
public class JoinWorldMessageSystem : NetworkServerMessageSystem<JoinWorldMessage>
4
{
5
protected override bool RequiresAuthentication() => true;
6
protected override void OnMessage(int connectionId, JoinWorldMessage message)
7
{
8
// handle the message
9
}
10
}
Copied!
Almost all server messages should require authentication. For example, a client that hasn’t logged in shouldn’t be able to send a player movement message.
Inherit from NetworkClientMessageSystem to handle a received message on the client:
1
using DOTSNET;
2
3
public class MyMessageSystem : NetworkClientMessageSystem<MyMessage>
4
{
5
protected override void OnMessage(MyMessage message)
6
{
7
// handle the message
8
}
9
}
Copied!
Last modified 20d ago