我有问题,我只想获得房间ID
我知道io.sockets.manager.rooms
但它返回对象:/
在代码中(我向客户发送房间列表,他检查 id)
io.sockets.on('connection', function(socket) {
socket.join('main');
socket.emit('start',io.sockets.manager.roomClients[socket.id]);
});
和客户:
var socket = io.connect('http://localhost:3250');
socket.on('start', function(data) {
panel.innerHTML = data;
}
解决方案是:
var keys = function( object ) {
if ( !(object && typeof object === 'object') ) {
return null;
}
var result = [];
for (var key in object) {
if (object.hasOwnProperty(key)) {
result.push(key)
}
}
return result;
}
var obiekt = io.sockets.manager.rooms;
socket.emit('rooms', keys(obiekt))