Creating, sending and receiving binary messages - BeardedManStudios/ForgeNetworkingRemastered GitHub Wiki

To send a binary frame, first create a BMSByte with the ObjectMapper, giving it the data you want to send. You can also add data to the byte with Append. Append takes a byte[] so we first need to convert our input.

BMSByte bytes = ObjectMapper.BMSByte(1, "a string", 2.0f);
bytes.Append(BitConverter.GetBytes(5));
bytes.Append(Encoding.ASCII.GetBytes("add a string"));

Then create the Frame.Binary object and send it

int MY_GROUP_ID = MessageGroupIds.START_OF_GENERIC_IDS + 1;
ulong timestep = client.Time.Timestep;
bool isTcpClient = client is TCPClient;
bool isTcp = client is BaseTCP;
Binary bin = new Binary(timestep, isTcpClient, bytes, Receivers.Target, MY_GROUP_ID, isTcp);
client.Send(bin);

Upon receiving the data, it is possible to get the data like this:

server.binaryMessageReceived += (player, msg, sender) =>
{
    if (msg.GroupId == MessageGroupIds.START_OF_GENERIC_IDS + 1)
        {
            int number = msg.StreamData.GetBasicType<int>();
            string text = msg.StreamData.GetBasicType<string>();
            float f = msg.StreamData.GetBasicType<float>();
        }
   };
}

Note: you can replace server with client and vice versa, both TCP/UDPServer and -Client have the binaryMessageReceived event.

⚠️ **GitHub.com Fallback** ⚠️