Node.js并发函数

Node.js并发函数,node.js,Node.js,我正在制作一个应用程序,它需要从串行端口读取数据,将数据添加到队列的后面,然后通过socket.io从队列的前面发出 我的示例代码(目前尚未测试)如下: 它使数据排队的部分基本上是正常的,因为它是异步的。当串行端口上接收到数据时,将对其进行处理(排队) 我想要的是“发射”独立于其他任何事情发生。也就是说,只要队列中有数据,它就必须运行。这里有一个草稿,还没有测试过,请告诉我它是否有效 //includes var http = require('http'), io = require('soc

我正在制作一个应用程序,它需要从串行端口读取数据,将数据添加到队列的后面,然后通过socket.io从队列的前面发出

我的示例代码(目前尚未测试)如下:

它使数据排队的部分基本上是正常的,因为它是异步的。当串行端口上接收到数据时,将对其进行处理(排队)


我想要的是“发射”独立于其他任何事情发生。也就是说,只要队列中有数据,它就必须运行。

这里有一个草稿,还没有测试过,请告诉我它是否有效

//includes
var http = require('http'),
io = require('socket.io'),
serialport = require('serialport'),
listish = require('listish');

//initialize serial
var portname = '/devttyACM0';
var sp = new serialport();
sp.open(portname, {
    baudrate: 9600,
    databits: 8,
    parity: 'none',
    stopbits: 1,
    flowcontrol: false
});

//initialize socket.io
io.sockets.on('connection', function(socket){
    socket.on('msg', function(msg){
        console.log(msg);
    });
    socket.on('disconnect', function(){
        console.log('disconnected');
    });
    socket.on('requestingData', function(){
      var msg = queue.dequeue();
      if(msg === "" || msg === [] || msg === null){
        return false; // don't send 'msg' with no msg
      };
      // emit all data from the FRONT of the queue to all clients
      io.sockets.emit('msg', msg);

      // Or to only send msg to the socket that requested the data this time:
      // socket.emit('msg', queue.dequeue());
    });
}).listen(8888);

//initialize queue
var queue = new listish();

//enqueue all data received on the serial port
sp.on('data', function(data){
    queue.enqueue(data.tostring());
})

/***client.js***
套接字=io.connect('http://localhost:8888');
socket.on('connect',function(){
警报(“已连接!检查F12控制台”);
});
socket.on('msg',函数(数据){
$('').text(数据)
。附件($(文件));
});
window.setInterval(
函数(){
emit('requestingData');
}
, 200); // 在ms中

应该
io.sockets.emit('msg',queue.dequeue())是否从串行端口每隔n个字节执行一次?还是用定时器?或者,如果在sp可读流上得到一个
end
事件,那么有一次?@Plato我希望这个过程能够一次又一次地执行,独立于代码中的所有其他内容。是否可以使用“while(true)”循环并将所有部分保留在同一个模块中?我认为您对基于异步事件的编程理解不正确。队列在这里是无用的,除非你想要一个像柏拉图所说的
end
事件。您只需将从串行接口中获取的任何内容直接导入套接字。不可能继续运行它,这将“阻止”程序的执行,并阻止程序的其余部分运行。请解释,在什么情况下,套接字应该从串行端口发送数据?您的选项非常多,“当串行端口发出
数据
事件时”,“基于此函数,在某些
数据
事件上,而不是在其他事件上”,“当串行端口发出
结束
事件时”,“每n毫秒”,或者“当我从其他功能手动启动时”。正如@TC1所示,您的最佳选择可能是让客户端向服务器发送一个
requestingData
事件;然后服务器将在('requestingData',handleDataRequest)
上执行io.sockets.on。在
handleDataRequest
函数中,您可以获取部分或全部队列,将其打包到socket.io消息中,然后将其发送回请求它的客户端。
//includes
var http = require('http'),
io = require('socket.io'),
serialport = require('serialport'),
listish = require('listish');

//initialize serial
var portname = '/devttyACM0';
var sp = new serialport();
sp.open(portname, {
    baudrate: 9600,
    databits: 8,
    parity: 'none',
    stopbits: 1,
    flowcontrol: false
});

//initialize socket.io
io.sockets.on('connection', function(socket){
    socket.on('msg', function(msg){
        console.log(msg);
    });
    socket.on('disconnect', function(){
        console.log('disconnected');
    });
    socket.on('requestingData', function(){
      var msg = queue.dequeue();
      if(msg === "" || msg === [] || msg === null){
        return false; // don't send 'msg' with no msg
      };
      // emit all data from the FRONT of the queue to all clients
      io.sockets.emit('msg', msg);

      // Or to only send msg to the socket that requested the data this time:
      // socket.emit('msg', queue.dequeue());
    });
}).listen(8888);

//initialize queue
var queue = new listish();

//enqueue all data received on the serial port
sp.on('data', function(data){
    queue.enqueue(data.tostring());
})
// ***client.js***
socket = io.connect('http://localhost:8888');
socket.on('connect', function(){
  alert('Connected! Check F12 console');
});
socket.on('msg', function(data){
  $('<div></div>').text(data)
  .appendTo($(document));
});

window.setInterval(
function(){
  socket.emit('requestingData');
}
, 200); // in ms