Reputation: 69
I'm trying to pass a javascript object with socket.io. Client side
var cPlanes = {}; //stands for client Planes
var plane = new Plane(x, y, z, model, name); //A plane object I made filled with stuff
cPlanes[nickname] = plane; //Nickname is defined elsewhere
socket.emit("send planes", cPlanes);//socket is defined elsewhere too
So this is suppose to send the cPlane object to the server.
Server side code is here
var sPlanes = {}; //stands for server Planes
socket.on("send planes", function(planes){
sPlanes = planes;
console.log(planes); //Is where I noticed the problem
});
when I do console.log(cPlanes)
in the client side, I get this
It's three.js for people who are wondering. This is the correct output, notice how the type of the object is Plane But when I print out sPlanes from the server, which is suppose to equal cPlanes, I get this.
It may look correct, but it's missing tons of properties. Then I decided to try and send it back to the client side and see what I will get.
//Server side
io.sockets["in"](socket.room).emit("draw planes", sPlanes);
//Client side
socket.on("draw planes", function(planes){
cPlanes = planes;
for(var i in cPlanes){
console.log(cPlanes);
}
});
this is what i get.
Notice the difference between the first picture and the third. I think socket.io is converting my Object to something else. But I could be making a stupid mistake somewhere in there. I'm new with socket.io and node.js, so any help will be appreciated. Thanks :D
Upvotes: 3
Views: 6428
Reputation: 1133
somethinf like Apache Thrift woukd help. The idea is to keep "class" definitions on both side of client and server.
What you can do is to wrap your class object.append a will-never-be-used variabke to hint the class on the client side. Then on the sever side, creat a new object of the class accordingly. And assign all the values to it.
Upvotes: 0
Reputation: 707148
When sending object via socket.io, they are serialized into JSON which does NOT retain the type of the object or any of the methods of the object. It only retains the properties of the object.
You can work around this by adding an extra property that indicates the type of object and having the receiving side convert the JSON into the right type of object, but you will have to write code yourself in order to do this.
Since you generally don't need to send a whole live object, you may want to rethink what you are sending to the server and just send a few important properties from the object and then when you get those properties on the server, you will know that it just has a subset of the information that is needed by the server.
In your specific example, there are a bunch of embedded objects in the cPlane object that likely are not needed by the server at all (like a renderer
object and a camera
object) so you really should think about exactly which properties does the server actually need and create a new generic object that contains only those properties and send that.
For example, you might want to do this:
socket.emit("send planes", {x:x, y:y, z:z, name: name});
Since these seem to be the unique properties you started with for this particular object.
Upvotes: 7