when we use Socket, we need Know About join & Leave Room.
Here socket join "game" room.
"game" is room name. you may use any number as room name.
Let, Send Some Message to Players which are connected with "game" room.
Here "niceGame" is event Name.
now we Print All Room which are connected with socket.
Print all rooms which are connected With this.socket
whenever you want to check your socket is connected with Room.
Thanks.
io.on('connection', function(socket){
socket.join('game');
});
Here socket join "game" room.
"game" is room name. you may use any number as room name.
Let, Send Some Message to Players which are connected with "game" room.
// sending to all clients in 'game' room except sender
socket.to('game').emit('niceGame', "let's play a game");
Here "niceGame" is event Name.
now we Print All Room which are connected with socket.
Print all rooms which are connected With this.socket
console.log(socket.adapter.rooms);
whenever you want to check your socket is connected with Room.
if(self.socket.adapter.rooms['game']){
console.log("you are in room");
}
Thanks.
Comments
Post a Comment