Socket.IO:如何删除名称空间 [英] Socket.IO: How do I remove a namespace
问题描述
我需要能够即时构建和销毁socket.io名称空间.很容易找到有关如何创建名称空间的信息,但是对于如何删除/断开名称空间以释放其内存,我一无所获.
I need to be able to construct and destruct socket.io namespaces on-the-fly. It is easily to find information how to create a namespace, but I find nothing about how I remove/disconnect the namespace to release its memory.
说我已经运行了以下代码:
Say I got the following code already running:
var nsp = io.of('/my-namespace');
nsp.on('connection', function(socket){
console.log('someone connected'):
});
nsp.emit('hi', 'everyone!');
如何断开/删除上面创建的socket.io名称空间?
How do I disconnect/remove the socket.io namespace created above?
推荐答案
io.of
方法仅创建一个数组元素:
The io.of
method just creates an array element:
Server.prototype.of = function(name, fn){
if (String(name)[0] !== '/') name = '/' + name;
if (!this.nsps[name]) {
debug('initializing namespace %s', name);
var nsp = new Namespace(this, name);
this.nsps[name] = nsp;
}
if (fn) this.nsps[name].on('connect', fn);
return this.nsps[name];
};
因此,我假设您可以将其从套接字io中的数组中删除.我很快地对其进行了测试,并且似乎可以正常工作.已连接的套接字保持连接状态.
So I assume you could just delete it from the array in socket io. I tested it pretty quick and it seems to work. Sockets that are already connected, keep connected.
delete io.nsps['/my-namespace'];
连接到/my-namespace
然后回退到默认名称空间.我不知道这是否是一个很好的解决方案,但是也许您可以稍微尝试一下.
Connecting to /my-namespace
then falls back to the default namespace. I don't know if this is a good solution, but maybe you can play with this a little..
这篇关于Socket.IO:如何删除名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!