简体   繁体   English

发送数组时出现Socket.io错误

[英]Socket.io error in sending arrays

I have 3 variables to send to the client using socket.io, namely, mapRes & location . 我有3个变量要使用socket.io发送到客户端,即mapReslocation

server.js

var mapRes = {width : 720, height: 1040};
var location = [{x:100,y:100,z:100},{x:200,y:200,z:200}];
var obj = {'1':{x:100,y:200}}, '2':{x:200,y:100}};
io.on("connection", function(socket){
    socket.emit("mapRes",mapRes);
    socket.emit("location",location);
    socket.emit("object",obj);
}

client.html

socket.on('mapRes',function(message){
    var mapRes = message;
    console.log(mapRes);
});
socket.on('location',function(message){
    var location = message;
    console.log(location);
});
socket.on('object',function (message){
    var object = message;
    console.log (object);
});

At the client side (browser), the first variable mapRes is received as expected. 在客户端(浏览器),按预期接收到第一个变量mapRes

Object { width: 720, height: 1040 }

The second variable, however, is received as shown below: 但是,收到第二个变量,如下所示:

Array [ Object, Object]

The third object is received as below: 接收到的第三个对象如下:

Object { 1: Object, 2: Object}

Is there a way to receive these variables/objects properly? 有没有办法正确接收这些变量/对象?

It probably has been sent correctly, you're just not seeing the whole object. 它可能已正确发送,您只是看不到整个对象。

This is due to Firefox collapsing objects when logged to console: 这是由于Firefox登录到控制台时对象崩溃了:

Firefox控制台

If you click on the underlined object, the right panel should open and show you more details about the object. 如果单击带下划线的对象,则右侧面板应打开,并向您显示有关该对象的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM