gpt4 book ai didi

javascript - MaxListenersExceededWarning : Possible EventEmitter memory leak detected. 添加了 11 个消息监听器。使用 emitter.setMaxListeners() 增加限制

转载 作者:可可西里 更新时间:2023-11-01 06:32:59 26 4
gpt4 key购买 nike

我知道这可能会标记为重复的解决方案,但堆栈溢出的解决方案对我不起作用。

问题

(node:5716) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 message lis
teners added. Use emitter.setMaxListeners() to increase limit.

我的代码库很大,有时我会遇到这个错误,但我不知道为什么会这样。我试图增加听众限制,但不幸的是,它不起作用。

const EventEmitter = require('events');
const emitter = new EventEmitter()
emitter.setMaxListeners(50)

更新

经过一些浏览,我运行这个命令来跟踪警告

node --trace-warnings index.babel.js

原来是我的 socket.io 代码是我将 socket.io 与 Redis 一起使用的问题。这是错误

node:14212) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 message li
steners added. Use emitter.setMaxListeners() to increase limit
at _addListener (events.js:281:19)
at RedisClient.addListener (events.js:298:10)
at Namespace.<anonymous> (D:/newProject/services/socket.js:21:17)
at emitOne (events.js:115:13)
at Namespace.emit (events.js:210:7)
at Namespace.emit (D:\newProject\node_modules\socket.io\lib\namespace.js:213:10)
at D:\newProject\node_modules\socket.io\lib\namespace.js:181:14
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)

这是代码(但这段代码用于更具体的任务,它不会一直执行)。

const redis = require('redis');
const config = require('../config');
const sub = redis.createClient(config.REDIS.port, config.REDIS.host);
const pub = redis.createClient(config.REDIS.port, config.REDIS.host);

sub.subscribe('spread');

module.exports = io => {
io.on('connection', socket => {
/* To find the User Login */
let passport = socket.handshake.session.passport;

if (typeof passport !== 'undefined') {
socket.on('typing:send', data => {
pub.publish('spread', JSON.stringify(data));
});

sub.on('message', (ch, msg) => {
// This is the Exact line where I am getting this error
io.emit(`${JSON.parse(msg).commonID}:receive`, { ...JSON.parse(msg) });
});
}
});
};

最佳答案

Event Emitter 的默认限制是 10。您可以使用 emitter.setMaxListeners 增加它。我的建议是不要更改它,除非明确要求,因为您没有退订,听众会增加。现在到您的代码。

const redis = require('redis');
const config = require('../config');
const sub = redis.createClient(config.REDIS.port, config.REDIS.host);
const pub = redis.createClient(config.REDIS.port, config.REDIS.host);

sub.subscribe('spread');

module.exports = (io) => {
io.on('connection', (socket) => {
// this callback will be executed for all the socket connections.
let passport =
socket.handshake.session.passport; /* To find the User Login */

if (typeof passport !== 'undefined') {
socket.on('typing:send', (data) => {
pub.publish('spread', JSON.stringify(data));
});

// this is where you are subscribing for each and every socket connected to your server
sub.on('message', (ch, msg) => {
// this is the Exact line where I am getting this error

// whereas you are emitting messages on socket manager, not on the socket.
io.emit(`${JSON.parse(msg).commonID}:receive`, { ...JSON.parse(msg) });
});
}
});
};

现在,如果我们分析上面的代码,那么如果你打开 20 个套接字连接到你的服务器,它将订阅 20 次,这里出错了。现在,如果您的要求是在服务器级别监听发布在 Redis 上的消息,然后在 io 上发出它,那么您的代码应该如下所示

const redis = require('redis');
const config = require('../config');
const sub = redis.createClient(config.REDIS.port, config.REDIS.host);
const pub = redis.createClient(config.REDIS.port, config.REDIS.host);

sub.subscribe('spread');

module.exports = (io) => {
sub.on('message', (ch, msg) => {
// this is the Exact line where I am getting this error
io.emit(`${JSON.parse(msg).commonID}:receive`, { ...JSON.parse(msg) });
});

io.on('connection', (socket) => {
let passport =
socket.handshake.session.passport; /* To find the User Login */

if (typeof passport !== 'undefined') {
socket.on('typing:send', (data) => {
pub.publish('spread', JSON.stringify(data));
});
}
});
};

关于javascript - MaxListenersExceededWarning : Possible EventEmitter memory leak detected. 添加了 11 个消息监听器。使用 emitter.setMaxListeners() 增加限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50709059/

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