gpt4 book ai didi

javascript - Socket.io Chat 应用程序显示检测到 EventEmitter 内存泄漏。添加了 11 个听众。使用 emitter.setMaxListeners()

转载 作者:可可西里 更新时间:2023-11-01 07:09:32 27 4
gpt4 key购买 nike

我正在使用 socket.ionode.js 加上 mysql 用于私有(private)聊天应用程序,当我使用 socket.on('example', function (data){...}); 在这里抛出这样的错误代码。

 node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
at PoolConnection.EventEmitter.addListener (events.js:175:15)
at io.on.eventConnection (/Applications/MAMP/htdocs/mysite/node/server.js:72:15)
at Ping.onOperationComplete [as _callback] (/Applications/MAMP/htdocs/mysite/node/node_modules/mysql/lib/Pool.js:99:5)
at Ping.Sequence.end (/Applications/MAMP/htdocs/mysite/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
at Ping.Sequence.OkPacket (/Applications/MAMP/htdocs/mysite/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:105:8)
at Protocol._parsePacket (/Applications/MAMP/htdocs/mysite/node/node_modules/mysql/lib/protocol/Protocol.js:280:23)
at Parser.write (/Applications/MAMP/htdocs/mysite/node/node_modules/mysql/lib/protocol/Parser.js:73:12)
at Protocol.write (/Applications/MAMP/htdocs/mysite/node/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.Connection.connect (/Applications/MAMP/htdocs/mysite/node/node_modules/mysql/lib/Connection.js:96:28)
at Socket.EventEmitter.emit (events.js:96:17)

我的代码在 服务器端:-

function fetchMessages(data){
var sql = "SELECT `msg_descr`,`msg_to` FROM `message` WHERE (msg_to="+data[0].iChatUserId+" AND msg_from="+data[1]+") OR (msg_to="+data[1]+" AND msg_from="+data[0].iChatUserId+") ORDER BY msg_created_date DESC LIMIT 20";
eventConnection(sql, function(callback, rows){
if(callback){
users[data[1]].emit('Release Msg', {messages:rows, selfId:data[0].iChatUserId});
}
});
}

//when a user is actibe
socket.on('load Message', function(data, callback){
var repsondMsg = fetchMessages(data);
});

在客户端:-

 chatList.click(function(){
var __this = $(this);
targetData = [];
__userData = fetchUserData(__this);
myId = myImage.data('myid');
targetData.push(__userData, myId);
socket.emit('load Message', targetData);
checkExistance(targetData);
});

socket.on('Release Msg', function(data){
$.each(data.messages, function(){
$.each(this, function(k,v){
$('*[data-ichatid="'+data.selfId+'"]').children('.body_cht_box_ind').children('#chats').append($('<li>').text(v));
})
});
});

最佳答案

下面的链接有解释。 https://nodejs.org/docs/latest/api/events.html#events_emitter_setmaxlisteners_n

By default EventEmitters will print a warning if more than 10 listeners are added for a particular event. This is a useful default that helps finding memory leaks. Obviously, not all events should be limited to just 10 listeners. The emitter.setMaxListeners() method allows the limit to be modified for this specific EventEmitter instance. The value can be set to Infinity (or 0) for to indicate an unlimited number of listeners."

你可以使用这样的东西。

require('events').EventEmitter.defaultMaxListeners = Infinity;

关于javascript - Socket.io Chat 应用程序显示检测到 EventEmitter 内存泄漏。添加了 11 个听众。使用 emitter.setMaxListeners(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35062570/

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