我正在为我的聊天应用程序使用套接字 io 和节点。有两个问题
- 1) 套接字的 RAM 使用量持续增加
- 2)我使用 pm2 监控套接字,它启动并正常工作,RAM 使用量增加并且超过 100MB,然后套接字停止并且 pm2 自动重新启动套接字,然后 RAM 使用量显示近 50MB 并且用户无法发送消息。
socket.on('iAmOnline', 函数(数据){
if(typeof usernames[data.id] != 'undefined'){
delete usernames[data.id];
delete onlineClients[data.id];
if(typeof isChatScreen[data.id] != 'undefined'){
delete isChatScreen[data.id];
}
}
socket.username = data.id;
usernames[data.id] = data.id
onlineClients[data.id] = socket.id;
if(typeof data.isChatScreen != 'undefined'){
isChatScreen[data.id] = data.isChatScreen;
}else{
isChatScreen[data.id] = 1;
}
if(typeof usernames[data.id] != 'undefined'){
var allChats = getChats(data.id, function(chats) {
var a = chats.pendingMessage;
var b = chats.deliveredMessage;
var c = chats.readMessage;
for(var i=0;i<a.length;i++){
socket.emit("message", {'from':a[i]['userAId'],'to':a[i]['userBId'], 'msg':a[i]['message'],'key':a[i]['uniqueKey']});
}
for(var i=0;i<b.length;i++){
socket.emit("delivered", {'from':b[i]['userBId'],'to':b[i]['userAId'], 'localId':0,'key':b[i]['uniqueKey']});
}
for(var i=0;i<c.length;i++){
socket.emit("read", {'from':c[i]['userBId'],'to':c[i]['userAId'], 'localId':0,'key':c[i]['uniqueKey']});
}
});
allChats = null;
}
});
function getChats(userId, callback){
return request({
uri: "http://twango.me/api/v1/chats?userId="+userId,
method: "GET",
timeout: 10000,
followRedirect: true,
maxRedirects: 10
}, function(error, response, body) {
callback(JSON.parse(body).response);
});
}