Node.js发送服务器input到客户端。 内存泄漏警告。 我的错误是什么? 那么node.js如何运行代码呢?

服务器:

var net = require('net'); var stdin = process.openStdin(); var client_list = []; var server = net.createServer(function(connection) { //console.log('client connected'); connection.on('error', function(e){ if(e.code == 'ECONNRESET'){ console.log('Client dissconeccted'); } }); //connection.write('Hello World!\r\n'); stdin.addListener("data", function(d) { // note: d is an object, and when converted to a string it will // end with a linefeed. so we (rather crudely) account for that // with toString() and then trim() console.log("you entered: [" + d.toString().trim() + "]"); connection.write(d.toString().trim()); }); connection.pipe(connection); }); server.listen(9999, function() { console.log('server is listening'); }); 

客户:

 var net = require('net'); var HOST = 'localhost'; var PORT = 9999; //var client = new net.Socket(); var client = net.connect(PORT, HOST, function(){ console.log('connected to server! ' + HOST + ':' + PORT); //client.write('I am Superman'); }); client.on('data', function(data) { var data = data.toString(); console.log(data); //If data starts with JS add injection functionallity if (data === "END"){ client.end(); console.log("ENDING!") } else if (data === "poo"){ console.log("HOLY SHIT!") } }); //Keep trying to connect! client.on('error', function(e) { console.log('Parent connection error'); //client.end(); client.connect(PORT, HOST); }); client.on('end', function() { console.log('disconnected from server'); }); /*var client = net.connect({port: 8080}, function() { console.log('connected to server!'); });*/ 

所以会发生的事情是,它不断添加监听器(?),并向11位监听者发出警告:

“检测到可能的EventEmitter内存泄漏,添加了11个数据侦听器,使用emitter.setMaxListeners()增加限制”。

为什么是这样? 我已经尝试通过移动stdin.addListener()解决这个问题,但是它要么根本不接受input,要么问题依然存在。 我上了什么? 代码如何在节点中运行,是一个循环?

提前致谢!

已经运行客户端和服务器脚本。 我无法重现您收到的错误消息。 我在Ubuntu上运行代码 – nodejs 6.9.5,npm 5.4.2。 你可以发布你的package.json文件的内容吗?

更新:看看网上。 看起来像Node中的一个已知的旧bug。 https://github.com/nodejs/node-v0.x-archive/issues/5108