2

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?

2 Answers 2

1

Normal binary serialization can be a bottleneck for Web API's If your objects are well defined and can be modeled into classes, i recommend you use Protobuf.

The great thing is that it's cross platform and you can use the same definition both for your NodeJS server and C# client.

Sign up to request clarification or add additional context in comments.

Comments

0

You can try binary json BSON http://bsonspec.org/implementations.html

or MSGPACK http://msgpack.org/index.html

both libraries offer implementation in various languages and are well tested.

3 Comments

I read about BSON parser in Node.js, it works much slower than JSON. I need to reduce CPU load on server side when encoding/decoding transfer data - its main target.
how come something that was written to be faster in parsing/writing than json is actually slower from your point of view ? if the data you exchange back and forth is large or is nested then might be the issue.
JSON de/encoding is probably implemented there in native code in optimized fashion directly in the JS VM. Yeap, i pass few nested arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.