节点js ECONNRESET events.js:72抛出

时间:2018-07-16 14:30:46

标签: node.js sockets tcp

您好,我正在使用Node js套接字。我正在通过IP和端口从多个设备监听数据。运行一段时间后,出现此异常,我的程序停止了

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: read ECONNRESET
    at errnoException (net.js:905:11)
    at TCP.onread (net.js:559:19)

这是我编写的代码

var net = require('net');
var request = require('request');



var net = require('net');
var HOST = '172.xx.x.xx';
var PORT = 52xx;
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
    // Fork workers.
    for (var i = 0; i < numCPUs; i++) {
        cluster.fork();
    }

    cluster.on('death', function(worker) {
        console.log('worker ' + worker.pid + ' died');
        cluster.fork();
    });
} else {
    net.createServer(function(socket){
         console.log('received connection...');
        socket.on('data', function(data) {

            var options = {
  uri: 'http://demourl/api',
  method: 'POST',
  body: {
    message:data.toString()
  },
  json: true 

};
             console.log(data.toString());

             request(options, function (error, response, body) {

});

        });


    }).listen(PORT, HOST);
}

如何捕获错误并阻止程序停止。

1 个答案:

答案 0 :(得分:0)

像这样包含socket.on("error", ...。看看this question

socket.on("error", (err) =>
    console.log("socket error: ")
    console.log(err.stack);
    socket.destroy();
)