套接字无法正常重启

时间:2016-08-28 13:37:24

标签: node.js sockets heap-memory pm2 npm-request

我在聊天应用程序中使用socket io和node。有两个问题

  • 1)通过套接字持续增加RAM使用量
  • 2)我使用pm2来监控套接字,它启动并正常工作,RAM使用量增加,超过100MB,然后套接字停止,pm2自动重启套接字,然后RAM使用量接近50MB,用户无法发送消息。

socket.on('iAmOnline',function(data){

    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);
});

}

附上Pm2日志。 enter image description here

0 个答案:

没有答案