0

I have 3 variables to send to the client using socket.io, namely, mapRes & location.

server.js

var mapRes = {width : 720, height: 1040};
var location = [{x:100,y:100,z:100},{x:200,y:200,z:200}];
var obj = {'1':{x:100,y:200}}, '2':{x:200,y:100}};
io.on("connection", function(socket){
    socket.emit("mapRes",mapRes);
    socket.emit("location",location);
    socket.emit("object",obj);
}

client.html

socket.on('mapRes',function(message){
    var mapRes = message;
    console.log(mapRes);
});
socket.on('location',function(message){
    var location = message;
    console.log(location);
});
socket.on('object',function (message){
    var object = message;
    console.log (object);
});

At the client side (browser), the first variable mapRes is received as expected.

Object { width: 720, height: 1040 }

The second variable, however, is received as shown below:

Array [ Object, Object]

The third object is received as below:

Object { 1: Object, 2: Object}

Is there a way to receive these variables/objects properly?

1
  • Are you sure, the object hasn't been sent? Assuming you're using firefox, its console sometimes displays objects weirdly (in a collapsed way). Try logging e.g. message[0].x or clicking on the objects. Commented Jun 17, 2015 at 7:40

1 Answer 1

1

It probably has been sent correctly, you're just not seeing the whole object.

This is due to Firefox collapsing objects when logged to console:

firefox console

If you click on the underlined object, the right panel should open and show you more details about the object.

Sign up to request clarification or add additional context in comments.

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.