Javascript 通过socket.io发送多个二维阵列时出现错误

Javascript 通过socket.io发送多个二维阵列时出现错误,javascript,arrays,node.js,sockets,socket.io,Javascript,Arrays,Node.js,Sockets,Socket.io,我注意到socket.io中有一个bug。我正在通过socket.io发送多(5)个二维阵列。如果与node.js或socket.io处理它们的方式有关,那么所有阵列都会以不同的长度/大小在不同的时间通过。但他们是分开的,所以我不认为这有什么关系,只是提一下 我有来自另一个来源的传入数据,它填充了Array1、Array2…等等。下面是通过socket.io发送时发生的情况的一些示例 当我发送test1时:test1确实在客户端填充 当我发送test2时:test2不在客户端填充 当我发送tes

我注意到socket.io中有一个bug。我正在通过socket.io发送多(5)个二维阵列。如果与node.js或socket.io处理它们的方式有关,那么所有阵列都会以不同的长度/大小在不同的时间通过。但他们是分开的,所以我不认为这有什么关系,只是提一下

我有来自另一个来源的传入数据,它填充了Array1、Array2…等等。下面是通过socket.io发送时发生的情况的一些示例

当我发送test1时:test1确实在客户端填充

当我发送test2时:test2不在客户端填充

当我发送test2时,然后test1:test1和test2在客户端填充

    var Array1 = [];
    var Array2 = [];        
    var Array3 = [];        
    var Array4 = [];
    var Array5 = [];

    jsonResults = JSON.stringify({
    'test1': Array1,
    'test2': Array2,
    'test3': Array3,
    'test4': Array4,
    'test5': Array5
    }); 
仅举一个我如何在客户端调用变量的示例

    var socket = io.connect();
    socket.on('message', function(message){ 
    var obj = jQuery.parseJSON(message);

    var value1 = obj.test1[1][1];
    var value2 = obj.test2[0][5];
我还有一个问题,如果在客户端调用一个不存在的变量,比如
obj.test1[5][5]
当test1只有[4,5]时,有时它不会在客户端填充,因为所有变量,比如
test1[1][1]
都存在;这有什么特别的原因吗?还是我的做法是错误的

有没有更好的方法从.js端编程我必须处理在不同时间出现的多个未知大小的二维数组

服务器端侦听外部通信

// start UDP server listening on port 3001
udpRunningResultsServer.on("listening", function () {
var address = udpRunningResultsServer.address();
console.log("UDP Running Results Server listening on port: " +    address.port);
console.log(" ")
});
udpRunningResultsServer.bind(3001);

// process datagram
udpRunningResultsServer.on("message", function (Runningmsg, Runninginfo) { 
// check if datagram is full (size=536)
if(Runninginfo.size == 536){
    tempRunningResults = tempRunningResults + Runningmsg.toString();
} 
// datagram is not full, so this is the end of the message
else{
    finalRunningResults = tempRunningResults + Runningmsg.toString();

    RunningArray = finalRunningResults.split(';');          
    for (var a = 0; a < RunningArray.length; a++) {       
        var Runningtmp = RunningArray[a].length;              
        if (Runningtmp == 14 || Runningtmp == 0){
            RunningArray.splice(a,1);                  
        }
        else {
            RunningArray[a]= RunningArray[a].split(',');             
        }
    }
    RunningArray.unshift(time);
    tempRunningResults ='';                         
}
});

// start UDP server listening on port 3002
udpShotPutWeightThrowResultsServer.on("listening", function () {
var address = udpShotPutWeightThrowResultsServer.address();
console.log("UDP Shotput/Weight Throw Server listening on port: " + address.port);
console.log(" ")
});
udpShotPutWeightThrowResultsServer.bind(3002);

  // process datagram
  udpShotPutWeightThrowResultsServer.on("message", function   (ShotputWeightThrowmsg, ShotputWeightThrowinfo) { 

    tempShotputWeightThrowResults = ShotputWeightThrowmsg.toString();
    finalShotputWeightThrowResults = finalShotputWeightThrowResults+tempShotputWeightThrowResults;
    finalShotputWeightThrowResults = finalShotputWeightThrowResults.trim()
    console.log(finalShotputWeightThrowResults);
    //if (tempShotputWeightThrowResults == '~'){
    console.log(finalShotputWeightThrowResults);
    ShotputWeightThrowArray = finalShotputWeightThrowResults.split(';');            
    for (var a = 0; a < ShotputWeightThrowArray.length; a++) {    =
            ShotputWeightThrowArray[a]=      ShotputWeightThrowArray[a].split(',');         
        }
var serv_io = io.listen(htmlServer, { log: false });
serv_io.sockets.on('connection', function(socket){
//send data to client
setInterval(function(){
    jsonResults = JSON.stringify({
    'runningobj': ShotputWeightThrowArray,
    'shotputweightthrowobj': RunningArray,
    'longtriplejumpobj': LongTripleJumpArray,
    'highjumpobj': HighJumpArray,
    'polevaultobj': PoleVaultArray
    });                         
    console.log(jsonResults);
    socket.send(jsonResults);   
}, 1000);
});

发送和接收数据的代码在哪里?我添加了服务器侦听端。我通过JSONSTRINGFI发送(..如上图所示。然后它在客户端接收数据,如上图所示。我知道将数据报转换为字符串的逻辑在这两个方面是不同的,但这是因为它们的形式不同,所以我不得不这样做。最终结果如下所示。我理解,您看到的结果可能与数组混淆n个阵列(可能不能这样做)。但如果我说在单独的脚本上运行它们,它确实正确。我希望它们一起运行,因为我不想同时启动多个节点服务器。
{"runningobj":["0.0",["UNOFFICIAL","Decat Men 110 Meter Hurdles","nwi","41","6","4","041-6-04","AUTO","4"],["","2","504","Klaus Ambrosch","Arizona","","","","","","","","","",""],["","4","693","Troy McDonough","Montana","","","","","","","","","",""],["","6","818","Pat Buckheit","Tennessee","","","","","","","","","",""],["","8","746","James Cook","Penn State","","","","","","","","","",""],",,,,,,,,,,,,,,",",,,,,,,,,,,,,,",",,,,,,,,,,,,,,",",,,,,,,,,,,,,,",",,,,,,,,,,,,,,",",,,,,,,,,,,,,,",",,,,,,,,,,,,,,",",,,,,,,,,,,,,,",",,,,,,,,,,,,,,",",,,,,,,,,,,,,,",["\u0003\u0004"]],"shotputweightthrowobj":[["\u0001R\u0002Unofficial","HammerThrowWomen","","19","1","1","019-1-01",""],["1","1","79","Crozier","Unattached","33.55","","110'01\""],["~"],["","","","","","","",""],["~"],["","","","","","","",""],["~"],["","","","","","","",""],["~"]],"longtriplejumpobj":[],"highjumpobj":[],"polevaultobj":[]}