I would like to create a random room ID on the server side and emit it to the client on connect, like below:
io.sockets.on('connection', function(socket){
var roomID = Math.random().toString(36).substring(2, 13);
io.emit('message', roomID);
socket.on('subscribe', function(room) {
socket.join(room);
})
});
Then, once the client has the server-created room ID, I would like to join the client to that room.
var socket = io.connect();
var lobby;
socket.on('message', function (data) {
lobby=data;
});
socket.emit('subscribe', lobby);
The problem is that I'm not able to pull the lobby
variable out of the socket.on()
function. Which, if I understand correctly, is because it's inside the anonymous function, and that makes it asynchronous.
Is there any solution here, or am I stuck with joining a room by giving it a name on the client side?
via Dshiz
No comments:
Post a Comment