Remove objects on disconnect socket.io

后端 未结 1 1406
暖寄归人
暖寄归人 2021-02-07 06:27

I\'m using Nodejs and Socket.io. When the client connects, new JavaScript objects are created.

Do these objects just linger forever? Should they be deleted or removed w

相关标签:
1条回答
  • 2021-02-07 06:42

    If you don't cleanup, then yes, they will stay there forever since I assume you are making them global.

    You should cleanup once a user disconnects by binding to the disconnect event listener:

    var clients = {}
    sockets.on('connection', function(socket) {
      clients[socket.id] = socket;
    
      socket.on('disconnect', function() {
        delete clients[socket.id];
      });
    });
    
    0 讨论(0)
提交回复
热议问题