I want to do something like:
var room = io.sockets.in(\'some super awesome room\');
room.on(\'join\', function () {
/* stuff */
});
room.on(\'leave\', functi
Recently troubled with the same problem. The code below should be the answer to what you're looking for. It may not be the most elegant solution, but works.
The catch is instead of using socket.join("something")
You need to use something like this this.roomHandler.room(socket,"room1","join")
.
class myserver{
constructor(){
this.io = require('socket.io')(85);
this.io.on('connection', (socket) => {
console.log("New User")
this.roomHandler.room(socket,"room1","join")
this.roomHandler.room(socket,"room2","join")
//this.roomHandler.room(socket,"room3","join")
this.roomHandler.room(socket,"room3","leave")
console.log("---")
console.log(this.roomHandler.roomsOfUser)
socket.on('disconnect', (reason) => {
this.roomHandler.disconnect(socket, reason)
console.log(this.roomHandler.roomsOfUser)
})
})
//Room Event Handler Definition
this.roomHandler = {
disconnect:(socket, reason)=>{
if(this.roomHandler.roomsOfUser[socket.id]){
this.roomHandler.roomsOfUser[socket.id].forEach(room => {
this.roomHandler.room(socket, room, "disconnect")
})
delete this.roomHandler.roomsOfUser[socket.id];
}
},
roomEvents : {},
roomsOfUser: {},
room:(socket,room,action)=>{//Join Or Leave
if(typeof socket == "object" && typeof socket.join == "function"){
if(typeof room=="string"){
//Room Join
if(action=="join"){
socket.join(room)
this.roomHandler.roomOn(room,"join",socket)
//Create, append room collection for user
if(this.roomHandler.roomsOfUser[socket.id]){
this.roomHandler.roomsOfUser[socket.id].push(room)
}else{
this.roomHandler.roomsOfUser[socket.id] = [room]
}
//Room Leave
}else if(action == "leave"){
if(this.roomHandler.roomsOfUser[socket.id][room]){//Really in the room?
socket.leave(room)
this.roomHandler.roomOn(room,"leave", socket)
}
//User Disconnected
}else if(action == "disconnect"){
this.roomHandler.roomOn(room,"leave", socket)
}else{
console.log("Undefined room action.")
}
}else{ console.log("Unqualified name for room."); }
}else{ console.error("Not a legit socket object",socket); socket.join("aaa") }
},
roomOn:(room, event, socket)=>{
if (typeof this.roomHandler.roomEvents[room] == "function"){
this.roomHandler.roomEvents[room](event,socket)
}else{
console.log(`No event found for ${room}`, this.roomHandler.roomEvents)
}
},
roomRegister:(room,callback)=>{
if (typeof room == "string" && typeof callback == "function") {
console.log(`Callback registered for ${room}`)
this.roomHandler.roomEvents[room] = callback
}else{
console.log("Room name or callback is invalid.")
}
}
}
//END OF HANDLER
//Register Functions for room events.
this.roomHandler.roomRegister("room1",(event,socket)=>{
console.log(`${event} for room1 BY ${socket.id}`)
})
this.roomHandler.roomRegister("room2",(event,socket)=>{
console.log(`${event} for room2 BY ${socket.id}`)
})
this.roomHandler.roomRegister("room3",(event,socket)=>{
console.log(`${event} for room3 BY ${socket.id}`)
})
}
}
const server = new myserver();