关于node.js net模块中 Use emitter.setMaxListeners() to increase limit 问题
发布于 8 年前 作者 Question-h 8218 次浏览 来自 问答

我想使用nodejs做tcp通信 我使用了下面的代码做测试: var net = require(‘net’);

var HOST = ‘127.0.0.1’;
var PORT = 6969;
var tcpserver= net.createServer(function(sock) { console.log('CONNECTED: ’ + sock.remoteAddress + ‘:’ + sock.remotePort);
sock.on(‘data’, function(data) {
console.log('DATA ’ + sock.remoteAddress + ': ’ + data);
sock.write(‘You said "’ + data + ‘"’);
});

sock.on('close', function(data) {  
    console.log('CLOSED: ' +  sock.remoteAddress + ' ' + sock.remotePort);  
});  

}) tcpserver.listen(PORT, HOST);

console.log(‘Server listening on ’ + HOST +’:’+ PORT);

当连接到11个客户端的时候提示 warning: possible EventEmitter memory leak detected. 11 uncaughtException listeners added. Use emitter.setMaxListeners() to increase limit.

我尝试添加sock.setMaxListeners(0); 还是无效 请教下用过net做TCP通信的朋友告诉下应该怎么做 nodejs的版本是4.4.5

1 回复
// By default EventEmitters will print a warning if more than 10 listeners are
// added to it. This is a useful default which helps finding memory leaks.
EventEmitter.defaultMaxListeners = 10;

默认是10个,需要修改的话,调用 setMaxListeners, 参数不是0,而是最大个数。

回到顶部