Sunday, 30 April 2017

pass Unit8Array from client to server nodejs with socket.io

I want to send a Unit8Array from client (browser) to server nodejs with socket.io. But, I had a problem that the type of data which server received is Object, not Unit8Array. Example code on client: (on client i use: )

var datasend = new Uint8Array(3);
        datasend[0] = 1;
        datasend[1] = 3;
        datasend[2] = 8;
if (datasend instanceof Uint8Array || Array.isArray(datasend))
            console.log("pass condition");
// in cosole, it logs "pass condition"
socket.emit('request', datasend);

Example code on Server: (on server i use socket.io version 1.7.3)

exports.onRequest = function (data, resp) {

    console.log("receive request from client");

    var a = typeof data;

    if (data instanceof Uint8Array) {
        console.log("type is ok");
    }
    else {
        console.log("type is not ok");
    }
    // server print out: type is not ok
    // if i insert the code var a = typeof data;
    // the value of a is "object"
};

So, Can you help me how to send this type of data to server? Thank you for you help!



via TccHtnn

No comments:

Post a Comment