小编use*_*868的帖子

Socket.io传递javascript对象

我正在尝试通过socket.io传递一个javascript对象。客户端

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
Run Code Online (Sandbox Code Playgroud)

因此,这假设是将cPlane对象发送到服务器。

服务器端代码在这里

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

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

在此处输入图片说明

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

在此处输入图片说明

它可能看起来正确,但是缺少大量的属性。然后,我决定尝试将其发送回客户端,看看会得到什么。

//Server side
io.sockets["in"](socket.room).emit("draw planes", sPlanes);

//Client side
socket.on("draw planes", function(planes){
    cPlanes …
Run Code Online (Sandbox Code Playgroud)

javascript sockets node.js socket.io

3
推荐指数
1
解决办法
4055
查看次数

标签 统计

javascript ×1

node.js ×1

socket.io ×1

sockets ×1