【问题标题】:Why does Node.js exit without an Exception after 2 - 5 minutes为什么 Node.js 在 2 - 5 分钟后退出而没有异常
【发布时间】:2015-10-23 04:10:46
【问题描述】:

EDIT-- 节点在 Windows 8.1 环境中运行。 我正在使用串口模块和 socket.io 从 Arduino 读取数据。我在 2 - 5 分钟内获得精美的数据。然后节点停止运行,没有抛出异常,我一无所知。我试过只使用“http”模块和“express”。无论哪种方式,结果都相同。完整的源代码如下。


server.js

var express = require('express');
var app = express();
var server = app.listen(3000, function () {

var host = server.address().address
var port = server.address().port

console.log('Example app listening at http://%s:%s', host, port)

});
var io = require('socket.io').listen(server);
var serialport = require('serialport');
var SerialPort = serialport.SerialPort;
var serialPort = new SerialPort("COM3",{
baudrate:9600,
dataBits: 8,
parity: 'none',
flowControl: false,
parser: serialport.parsers.readline("<EOD>")
});


app.use(express.static(__dirname));

app.get('/', function (req, res) {
res.render('./index.html')
});

serialPort.on('open', function(){
// Now server is connected to Arduino
console.log('Serial Port Opened');

 var lastValue;
io.sockets.on('connection', function (socket) {
  //Connecting to client 
  console.log('Socket connected');
  //socket.emit('connected');
  var lastValue;

      serialPort.on('data', function(data){
      //if(lastValue !== data.toString()){
          socket.emit('data', data.toString());
          console.log(data.toString());
      //}
      lastValue = data.toString();
    });
  });
});

index.html 脚本:包含所有 socket.io 功能。

<script>

    function handleIO(){

        function serverDisconnect(){
            console.log("Server Disconnected.");
            console.log(new Date().toTimeString());
        }
        socket.timeout = 60*60*1000;

        socket.on('disconnect', serverDisconnect);

        socket.on('data', function(data) {
            //console.log("Got Data");
            console.log(data);
            render(data);   
        });

        console.log("Connected To Server.");
        console.log(new Date().toTimeString());
    }

    var socket = io();

    socket.on('connect', handleIO);


</script>

编辑 #2:控制台日志:

C:\Users\Dave\Desktop\js\js\Websensors2>node serverexp.js
Example app listening at http://:::3000
Port open
New Socket Client connected: JPozMKfwH77x8uMNAAAA
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.80
Sent data to connected Socket clients
74.77
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.77
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.41
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.41
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.55

C:\Users\Dave\Desktop\js\js\Websensors2>

【问题讨论】:

    标签: node.js socket.io node-serialport


    【解决方案1】:

    我想你有同样的问题。我在3天前(2.0.4)serialport发布新版本时解决了。

    我在串口 github 上的问题:https://github.com/voodootikigod/node-serialport/issues/609

    现在尝试使用 npm 重新安装串口模块,完全兼容 node 4.2.2 LTS :)

    【讨论】:

      【解决方案2】:

      这似乎与串行端口库的至少一个已知问题有关。似乎最好的短期修复是回滚到串行端口的先前版本。该问题记录在 github 上。谢谢你的帮助。希望我在发帖之前就找到了。

      【讨论】:

        【解决方案3】:

        我对您的源代码“server.js”进行了一些更改,请使用下面提供的修改后的源代码。我已经包含了单独的事件来记录 Socket.IO 和串行端口的不同事件。

        http:在 3000 端口运行
        socket.io:在端口中作为 http
        运行 serialPort:通过串口连接服务器

        1. 打开 http 连接
        2. 打开串口
        3. 打开 Socket.IO
        4. 从串口接收数据
        5. 将接收到的数据从串口转发到 Socket IO 客户端

          server.js

          var express = require('express');
          var app = express();
          
          var SerialPort = require('serialport');
          var http = require('http').Server(app);
          var io = require('socket.io')(http);
          
          var options_serialPort = {
              baudrate: 9600,
              dataBits: 8,
              parity: 'none',
              flowControl: false,
              parser: serialport.parsers.readline("<EOD>")
          };
          
          var serialPort = new SerialPort("COM3", options_serialPort);
          
          app.use(express.static(__dirname));
          
          app.get('/', function (req, res) {
              res.render('./index.html')
          });
          
          serialPort.on('open', function () {
              // Callback called when serial port is opened
              console.log('Port open');
          });
          
          serialPort.on('data', function (data) {
              // Callback called when serial port is receiving data from Arduino
              sendReceivedDataToSocketIOClients(data);
              console.log(data.toString());
          });
          
          serialPort.on('close', function (error) {
              // Callback called when serial port is closed
              console.log('Port closed');
          });
          
          serialPort.on('error', function (error) {
              // Callback called when serial port receives any error
              console.log('Error in Port: ' + error);
          });
          
          function sendReceivedDataToSocketIOClients(data) {
          
              // sending to all socket clients
              io.emit('data', data.toString());
              console.log('Sent data to connected Socket clients');
          
              //console.log('No Socket clients connected to send data');
          }
          
          io.on('connection', function (socket) {
              console.log("New Socket Client connected: " + socket.id);
          
              socket.on("connect", function () {
                  console.log("Socket Client Connected!");
              });
          
              socket.on('disconnect', function () {
                  console.log("Socket Client Disconnected!");
              });
          
          });
          
          http.listen(3000, function () {
              var host = server.address().address;
              var port = server.address().port;
          
              console.log('Example app listening at http://%s:%s', host, port);
          });
          

        【讨论】:

        • 运行新的源代码,同样的问题。它在 3 分钟后退出而不抛出任何异常......
        • @DavidPolly 请提供控制台日志以继续并进一步调查
        • 控制台日志已添加到原始帖子中。
        猜你喜欢
        • 2015-08-01
        • 1970-01-01
        • 2011-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多