node.js - Socket.io passing javascript object -


i'm trying pass javascript object socket.io. client side

var cplanes = {}; //stands client planes var plane = new plane(x, y, z, model, name); //a plane object made filled stuff cplanes[nickname] = plane; //nickname defined elsewhere socket.emit("send planes", cplanes);//socket defined elsewhere 

so suppose send cplane object server.

server side code here

var splanes = {}; //stands server planes socket.on("send planes", function(planes){     splanes = planes;     console.log(planes); //is noticed problem }); 

when console.log(cplanes) in client side, this

enter image description here

it's three.js people wondering. correct output, notice how type of object plane when print out splanes server, suppose equal cplanes, this.

enter image description here

it may correct, it's missing tons of properties. decided try , send client side , see get.

//server side io.sockets["in"](socket.room).emit("draw planes", splanes);  //client side socket.on("draw planes", function(planes){     cplanes = planes;     for(var in cplanes){         console.log(cplanes);     } }); 

this get.

enter image description here

notice difference between first picture , third. think socket.io converting object else. making stupid mistake somewhere in there. i'm new socket.io , node.js, appreciated. :d

when sending object via socket.io, serialized json not retain type of object or of methods of object. retains properties of object.

you can work around adding property indicates type of object , having receiving side convert json right type of object, have write code in order this.

since don't need send whole live object, may want rethink sending server , send few important properties object , when properties on server, know has subset of information needed server.


in specific example, there bunch of embedded objects in cplane object not needed server @ (like renderer object , camera object) should think properties server need , create new generic object contains properties , send that.

for example, might want this:

socket.emit("send planes", {x:x, y:y, z:z, name: name}); 

since these seem unique properties started particular object.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -