Socket.io 传递 javascript 对象 [英] Socket.io passing javascript object

查看:29
本文介绍了Socket.io 传递 javascript 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 socket.io 传递一个 javascript 对象.客户端

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

所以这是假设将 cPlane 对象发送到服务器.

So this is suppose to send the cPlane object to the server.

服务器端代码在这里

var sPlanes = {}; //stands for server Planes
socket.on("send planes", function(planes){
    sPlanes = planes;
    console.log(planes); //Is where I noticed the problem
});

当我在客户端执行 console.log(cPlanes) 时,我得到了这个

when I do console.log(cPlanes) in the client side, I get this

对于想知道的人来说,这是three.js.这是正确的输出,注意对象的类型是 Plane但是当我从服务器打印出 sPlanes(假设等于 cPlanes)时,我得到了这个.

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);
    }
});

这就是我得到的.

注意第一张图片和第三张图片之间的区别.我认为 socket.io 正在将我的对象转换为其他东西.但我可能在那里的某个地方犯了一个愚蠢的错误.我是 socket.io 和 node.js 的新手,所以任何帮助将不胜感激.谢谢:D

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

推荐答案

通过 socket.io 发送对象时,它们被序列化为 JSON,不保留对象的类型或对象的任何方法.它只保留对象的属性.

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.

您可以通过添加一个指示对象类型的额外属性并让接收方将 JSON 转换为正确类型的对象来解决此问题,但您必须自己编写代码才能执行此操作.

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.

在您的具体示例中,cPlane 对象中有一堆嵌入的对象,它们可能根本不需要服务器(例如 renderer 对象和 camera> 对象),因此您确实应该考虑服务器实际需要哪些属性,并创建一个仅包含这些属性的新通用对象并将其发送.

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.

这篇关于Socket.io 传递 javascript 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆