删除方括号并在nodejs/websocket环境中正确接收JSON? [英] Remove a square bracket and receiving JSON properly in nodejs/websocket enviroment?

查看:130
本文介绍了删除方括号并在nodejs/websocket环境中正确接收JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我只能使用console.log(auction[index].id);输出特定键(索引)的数据,但我需要使用console.log(auction.id);进行数据输出.我该怎么做?

At the moment I can only output data of a specific key (index) with console.log(auction[index].id); but I need the data using console.log(auction.id);. How do I accomplish this?

我使用的是nodejs,我的WebSocket接收数据的方式如下:

I'm using nodejs and my WebSocket receives data just like this:

42["stream",{"streamArray":[[{"id":20,"pid":2,"nr":4,"price":5.73,"pe":506.08,"duration":14,"hb":361262},{"id":23,"pid":17,"nr":4,"price":5.59,"pe":189.13,"duration":7,"hb":null},{"id":12,"pid":8,"nr":3,"price":5.59,"pe":90.23,"duration":7,"hb":null}]]}]

所以它是一个多维数组.我认为方括号太多了吗? (例如,在{"id":20之前)

So it's a multidimensional array. And I think there's one square bracket too much? (Right before {"id":20 for example)

通过这种方式,我可以接收客户端的数据:

This way I receive the data client-side:

var socket = io.connect('http://localhost:8000/',{'forceNew':true });
socket.on('stream', function (data) {
$.each(data.streamArray,function(index,auction){    

在将数据发送到WebSocket之前我是否必须更改代码,还是可以解决此问题,以便客户端在变量auction中接收正确的数据?我该怎么解决?

Do I have to change the code before the data is even sent to my WebSocket or can I solve this issue client-side to receive the correct data in my variable auction? How could I solve this?

这是相关的服务器端代码:

This is the relevant server-side code:

function pollingLoop () {
    if (socketArray.length === 0) {
        // no connections, wait and try again
        setTimeout(pollingLoop, POLLING_INTERVAL);
        return; // continue without sending mysql query
    }
    pool.getConnection(function(err,connection){ 
    if (err) { console.log({"code" : 100, "status" : "connection-db error"}); return; }   
    console.log('connected as ' + connection.threadId);

    var selection = 
        "SELECT * FROM auctions";   
    var streamArray = [], lg = '';  // init of array and log variable                   
        var query = connection.query(selection, function(err, fields, results){

        for (i=0; i < fields.length; i++)
        {
            lg += (fields[i].id+' ('+fields[i].duration+') '+fields[i].price+'€ -');    

            if (somecondition)  
            {
            // mysql update here
            }       
        }
        streamArray.push(fields);
        updateSockets({ streamArray: streamArray });    
        connection.release();
        setTimeout(pollingLoop, POLLING_INTERVAL);  
        console.log(time()+lg+' C: '+socketArray.length);
        });
    }); 
    }
        pollingLoop();

io.sockets.on('connection', function(socket) {  
    socket.on('disconnect', function() {
    clearTimeout(pollingTimer);
    var socketIndex = socketArray.indexOf(socket);
    console.log(time()+'SOCKET-ID = %s DISCONNECTED', socketIndex);
    if (~socketIndex) { socketArray.splice(socketIndex, 1); }
    });  
    console.log(time()+'NEW SOCKET CONNECTED!');
    socketArray.push(socket);
}); 

var updateSockets = function(data) {
    socketArray.forEach(function(tmpSocket) { tmpSocket.volatile.emit('stream', data); });
};

推荐答案

这是您的问题:

    streamArray.push(fields);
    updateSockets({ streamArray: streamArray });

它确实创建了多余的一槽阵列.我的猜测是,您要么想将streamArray.push(…)移到for循环内(然后按fields[i]或类似的东西),要么应该这样做

It does create the one-slot array that is superfluous. My guess is that you either wanted to move the streamArray.push(…) inside of the for loop (and then push fields[i] or something), or you just should do

 updateSockets({ streamArray: fields });

,并忽略所有streamArray代码.

这篇关于删除方括号并在nodejs/websocket环境中正确接收JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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