删除方括号并在 nodejs/websocket 环境中正确接收 JSON?
Remove a square bracket and receiving JSON properly in nodejs/websocket enviroment?
目前我只能使用 console.log(auction[index].id);
输出特定键(索引)的数据,但我需要使用 console.log(auction.id);
的数据。我该如何实现?
我正在使用 nodejs,我的 WebSocket 接收数据就像这样:
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
之前)
这样我在客户端接收数据:
var socket = io.connect('http://localhost:8000/',{'forceNew':true });
socket.on('stream', function (data) {
$.each(data.streamArray,function(index,auction){
我是否必须在数据发送到我的 WebSocket 之前更改代码,或者我能否在客户端解决此问题以在我的变量 auction
中接收正确的数据?我该如何解决这个问题?
编辑:
这是相关的服务器端代码:
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]
或其他东西),要么你应该做
updateSockets({ streamArray: fields });
并省略所有 streamArray
代码。
目前我只能使用 console.log(auction[index].id);
输出特定键(索引)的数据,但我需要使用 console.log(auction.id);
的数据。我该如何实现?
我正在使用 nodejs,我的 WebSocket 接收数据就像这样:
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
之前)
这样我在客户端接收数据:
var socket = io.connect('http://localhost:8000/',{'forceNew':true });
socket.on('stream', function (data) {
$.each(data.streamArray,function(index,auction){
我是否必须在数据发送到我的 WebSocket 之前更改代码,或者我能否在客户端解决此问题以在我的变量 auction
中接收正确的数据?我该如何解决这个问题?
编辑:
这是相关的服务器端代码:
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]
或其他东西),要么你应该做
updateSockets({ streamArray: fields });
并省略所有 streamArray
代码。