1

I am doing some experimenting with socket.io. I have a canvas that successfully sends data to the server which receives it fine.

It receives a Uint8ClampedArray which is correct because that is what is being sent.

When I then .send this message from the server to the client, I get a string: [object Object]. Again I have checked! Am I missing something, the code for the server is below:

var fs, http, io, server;
fs = require('fs');
http = require('http');
server = http.createServer(function(req, res) {
    return fs.readFile("" + __dirname + "/front.html", function(err, data) {
        res.writeHead(200, {
            'Content-Type': 'text/html'
        });
        return res.end(data, 'utf8');
    });
});
server.listen(1337);
io = require('socket.io').listen(server);
io.sockets.on('connection', function(socket) {
    socket.on('publish', function(message) {
         return io.sockets.send(message);
    });
});
5
  • 2
    did you tried serializing data before send - JSON.stringify ? Commented May 13, 2013 at 7:11
  • Convert this array to string or use post to send data Commented May 13, 2013 at 7:12
  • Ah thank you! JSON.stringify sends the data! Just need to figure out how to convert it back! Commented May 13, 2013 at 7:14
  • to convert back - JSON.parse() Commented May 13, 2013 at 7:24
  • Thanks, how do I convert it back to Uint8ClampedArray, that is all Canvas seems to accept. Commented May 13, 2013 at 8:05

2 Answers 2

9

On Client:

var dataStr = JSON.stringify(data); // converts object to string

On Server:

var dataObj = JSON.parse(data); // converts string to object
Sign up to request clarification or add additional context in comments.

5 Comments

I have to do the JSON.parse on the client when it receives the data because when I do it on the server, I get the same problem as before.
JSON.parse will convert back string to array so you can use at any place where you need this type of conversion. server will always return string so you will need to parse it with these two options I mentioned.
JSON.parse uses eval at the back, so I prefer using eval.
Waqar, sorry, eval is a security risk. Just to echo Christoph's outrage, running eval('client submitted data') is equivalent to saying, "hey, run any code you want on my server." It opens you up to every security hole imaginable. Don't do it!
You are right in that case... its just a matter of conversion. For security concerns I would only use JSON.parse
2

The only way is to reuse Uint8ClampedArray in your client size. If you have this on your server-side

var x = new Uint8ClampedArray(3);
x[0] = -17;
x[1] = 93;
x[2] = 350;
var dataThatWillBeSent = JSON.stringify(x);
// '{"0":0,"1":93,"2":255,"length":3,"byteLength":3,"byteOffset":0,"buffer":{"byteLength":3}}'

In your client side, assume that you have included Uint8ClampedArray, you can do this

var dataReceived = '{"0":0,"1":93,"2":255,"length":3,"byteLength":3,"byteOffset":0,"buffer":{"byteLength":3}}';
dataReceived = JSON.parse(dataReceived);
// reconstruct Uint8ClampedArray object

I have not used Uint8ClampedArray before, so I dont know exactly how you can recover a Uint8ClampedArray object from a JSON data, but if you read the document you might be able to figure something out

Comments

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.