【发布时间】:2017-10-19 01:35:15
【问题描述】:
我正在使用 NodeJS 和 WS 来测试 websocket。为了从服务器上卸载一些工作,我想将 ping 从客户端发送到服务器,而不是反过来。但是,每当我运行我的代码时,我都会收到此错误:
> (node) warning: possible EventEmitter memory leak detected. 11 pong listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
at WebSocket.addListener (events.js:239:17)
at Object.<anonymous> (/home/ubuntu/NodeJS-Runtime/websockets/client.js:40:12)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:442:10)
at startup (node.js:136:18)
at node.js:966:3
这是我的代码:
for(var i=0; i<20; i++) {
const clientNum = i;
const ws = new WebSocket('ws://localhost:8080', {
perMessageDeflate: false
});
ws.onerror = function(error) {
if(!hasFailed && clientNum != 0)
console.log('failed on: ' + clientNum + error);
hasFailed = true;
}
ws.on('open', function() {
// Send keep alive messages. Close if no response.
ws.keepAlive = false;
var interval = setInterval(function() {
if (ws.keepAlive) {
ws.close();
} else {
ws.ping(null, null, true);
ws.keepAlive = true;
}
}, 25*1000); // milliseconds between pings
});
ws.on("pong", function() {
ws.keepAlive = false;
});
pong 和 'on' 功能都出现此错误。
【问题讨论】:
-
我刚刚使用
ws@3.0.0和 node v 7.5.0 测试了此代码的重写,即使每个集群有 100 个客户端,我也没有收到任何警告。 Here's my attached code in a zip file for reference。抱歉,我无法重现您的问题。
标签: javascript node.js memory-leaks node-modules