Skip to content

Creating, sending and receiving binary messages

Erik Nilson edited this page Oct 2, 2018 · 4 revisions

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.

Home

Getting Started
Network Contract Wizard (NCW)
Network Object
Remote Procedure Calls (RPCs)
Unity Integration
Basic Network Samples
Scene Navigation
NetWorker
Master Server
Web Server
Netcoding Design Patterns
Troubleshooting
Miscellaneous
Clone this wiki locally
You can’t perform that action at this time.