Socket.IO按名称删除一个房间

时间:2017-07-11 10:47:42

标签: node.js socket.io

当它的名字不再需要时,有没有办法完全删除一个房间。 我试过以下答案但没有工作: https://stackoverflow.com/a/23342511 我正在使用socketio 2并抛出以下错误:

apiObservable

node_modules/socket.io-adapter/index.js:210

基本上,是否有删除/删除房间的快捷方式,而不是为每个套接字执行if (fn) process.nextTick(fn.bind(null, null, sids)); TypeError: fn.bind is not a function

1 个答案:

答案 0 :(得分:4)

这里我最终如何解决这个问题:

io.of('/').in('room name').clients(function(error, clients) {
    if (clients.length > 0) {
        console.log('clients in the room: \n');
        console.log(clients);
        clients.forEach(function (socket_id) {
            io.sockets.sockets[socket_id].leave('room name');
        });
    }
});

这应该适用于socket.IO版本2.x