Marco Sulla Marco Sulla - 1 month ago 17
Javascript Question

Unpack a C struct on browser?

EDIT



I found this:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays

Basically if I have something like this:

struct someStruct {
unsigned long id;
char username[16];
float amountDue;
};


on client side I can do:

var ws = new WebSocket("ws://URI");
ws.binaryType = "arraybuffer";

ws.onmessage = function (e) {
var buffer = e.data;
var data_view = new DataView(buffer);

// ... read the data into the buffer ...

var idView = data_view.getUint32(0);
var usernameView = data_view.getUint32(4);
var amountDueView = data_view.getFloat32(20);
};


The problem is that I want to convert them to normal Javascript objects (numbers, strings etc).

Original question



I would send data via websocket packed using a C struct, and unpack on browser using Javascript.

I know modules exists for node.js, but I can't find nothing client-side.

Answer

Ok, after some researching, I finally decided this could not be a good idea:

https://justin.harmonize.fm/development/2013/04/28/a-slower-js-msgpack.html

Shortly: javascript is slow in decoding.

Probably it's just more simple to use JSON and Content-Encoding: gzip, if it does not slow down your web app.

Comments