我从服务器端向客户端发送对象,并且客户端获得的对象与我发送的对象完全不同。这就是我的意思是:Socket IO客户端接收对象与服务器发送的内容不同
代码
服务器端代码:
//Basic setup for the example
var res={};
res.models=[];
res.models[0]={};
//The OLD (wrong) value
res.models[0]['id']="b4fd12f1-61ca-4445-b916-62617f1c0a78";
//The NEW (correct) value
res.models[0]['id']="fb8f83e2-e157-4e1e-8315-8e5f03b44691";
//Output what the id is, this is always the correct id!
console.log("res.models[0]['id']: ",res.models[0]['id']);
socket.emit("updateRealSeatSelect", res);
客户端代码:
socket.on("updateRealSeatSelect", function(data){
//Output the id when we get the object. This is always wrong!
console.log("res.models[0]['id']: "+data.models[0]['id']);
});
输出的问题。这里就是我得到:
输出
服务器端输出:(正确)
res.models[0]['id']: fb8f83e2-e157-4e1e-8315-8e5f03b44691
客户端输出:(!错了为什么不一样?)
res.models[0]['id']: b4fd12f1-61ca-4445-b916-62617f1c0a78
我hav e双重三重四倍检查确认服务器正在使用正确的ID发送正确的对象。 当客户端获取对象时,对象不同并且有错误的ID!这怎么会发生?
其它
尝试:
我试图复制
res
到一个新的对象,然后发送这一点,但也没有工作。就像这样:var newres = res; socket.emit("updateRealSeatSelect", newres);
我使用
JSON.parse
和JSON.stringify
当我发送的对象,但是这并没有帮助也试过。就像这样:socket.emit("updateRealSeatSelect", JSON.stringify(res));
版本:
- 快递版本:3.5.2
- 节点版本:V6.5.0
- Socket.IO:v1.3.6
如果您不指定错误的值会发生什么? –
@ PM77-1首先设置错误的值是显示socket io正在发送旧对象。在我的实际代码中,id被设置在其他地方,然后我替换该id并发送消息。所以我不能跳过这一步:(但是当我在服务器端输出对象时,这是正确的。我不知道为什么它在服务器端是正确的,而在客户端上是错误的。 – Kayvar
我没有提出任何建议。我只是想知道没有事先分配会发生什么,您可以检查吗? –