gpt4 book ai didi

node.js - nodejs setMaxListeners 避免内存泄漏检测

转载 作者:搜寻专家 更新时间:2023-10-31 23:30:10 27 4
gpt4 key购买 nike

我阅读了其他一些问题和帖子,但找不到在哪里应用 .setMaxListeners(0)。我正在使用一个简单的 websocket-server,它出现了错误:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
at Socket.EventEmitter.addListener (events.js:160:15)
at Socket.Readable.on (_stream_readable.js:689:33)
at XHRPolling.Transport.setHandlers (D:\nodeJS\host\node_modules\socket.io\lib\transport.js:116:15)
at XHRPolling.HTTPPolling.setHandlers (D:\nodeJS\host\node_modules\socket.io\lib\transports\http-polling.js:53:39)
at XHRPolling.Transport.handleRequest (D:\nodeJS\host\node_modules\socket.io\lib\transport.js:70:10)
at XHRPolling.HTTPTransport.handleRequest (D:\nodeJS\host\node_modules\socket.io\lib\transports\http.js:84:39)
at XHRPolling.HTTPPolling.handleRequest (D:\nodeJS\host\node_modules\socket.io\lib\transports\http-polling.js:73:41)
at XHRPolling.Transport (D:\nodeJS\host\node_modules\socket.io\lib\transport.js:31:8)
at XHRPolling.HTTPTransport (D:\nodeJS\host\node_modules\socket.io\lib\transports\http.js:29:13)
at XHRPolling.HTTPPolling (D:\nodeJS\host\node_modules\socket.io\lib\transports\http-polling.js:27:17)

我正在以这种方式实例化路由器:

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app, { log: false })
, fs = require('fs');
app.listen(8070);

然后我正在监听传入的套接字请求:

io.sockets.on('connection', function (socket) {

socket.on('createTake', function(data){...}
});

我可以在哪里应用 MaxListeners?

我已经试过了:

io.sockets.on(...).setMaxListeners(0);

但它不起作用。

最佳答案

您正在将监听器附加到 io.sockets (io.sockets.on(...))。并且您应该将 setMaxListeners 应用到同一个对象(到 EventEmmiter)。所以尝试:

io.sockets.setMaxListeners(0);

关于node.js - nodejs setMaxListeners 避免内存泄漏检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20066542/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com