Marco Sulla Marco Sulla - 7 months ago 53
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.


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.