I have Node.js WebSocket server and Unity client with Best HTTP asset. I used to transfer JSON data, but parsing cost too much CPU resources. Now, i try to send byte[] message from C# as follows:
//sending sample array - new float[] { 1.32, 3.12 }
//convert an object to a byte array
private byte[] ObjectToByteArray(Object obj)
{
if(obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
But server received Uint8Array [1, 3] and fractional part is lost.
var wss = new WebSocket.Server({ port: 8080 });
wss.binaryType = "arraybuffer";
wss.on('connection', function connection(ws) {
ws.on('message', function (message) {
});
});
Main question: How to right parse arrays (especially nested arrays) in binary format and encode to array after?