删除socket.io上的对象 [英] Remove objects on disconnect socket.io

查看:97
本文介绍了删除socket.io上的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Nodejs和Socket.io。当客户端连接时,会创建新的JavaScript对象。

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 when the client disconnects? Is it even possible to remove an object? I know delete won't work...

谢谢 - 我想这更像是一个普遍的问题,任何建议都会非常有用。

Thanks - I guess this is more of a general question and any suggestions would be really helpful.

谢谢!

推荐答案

如果你不清理,那么是的,他们将留在那里永远,因为我假设你把它们全局化。

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

一旦用户通过绑定到 disconnect 断开连接,你应该清理事件监听器:

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

这篇关于删除socket.io上的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆