问题描述
我正在使用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上的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!