Marco Sulla Marco Sulla - 1 year ago 112
Javascript Question

Unpack a C struct on browser?


I found this:

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 =;
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 Source

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

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download