javascript - Socket.io how to get room id -
i have problem, want only room id
i know io.sockets.manager.rooms
returns object :/
in code (i send list of rooms client , checks id)
io.sockets.on('connection', function(socket) { socket.join('main'); socket.emit('start',io.sockets.manager.roomclients[socket.id]); });
and client:
var socket = io.connect('http://localhost:3250'); socket.on('start', function(data) { panel.innerhtml = data; }
the solution is:
var keys = function( object ) { if ( !(object && typeof object === 'object') ) { return null; } var result = []; (var key in object) { if (object.hasownproperty(key)) { result.push(key) } } return result; } var obiekt = io.sockets.manager.rooms; socket.emit('rooms', keys(obiekt))
i'm not 100% sure need here. want return array of room names? if so, can keys object returned io.sockets.manager.rooms, e.g. using helper function provided underscore:
socket.emit('rooms', _.keys(io.sockets.manager.rooms) );
edit: i've used underscore here, can code own keys function, looping through objects properties, this:
var keys = function( object ) { if ( !(object && typeof object === 'object') ) { return null; } var result = []; (var key in object) { if (object.hasownproperty(key)) { result.push(key) } } return result; }
note might miss edge cases, idea
Comments
Post a Comment