In my game application I would like to have general class for handling socket connection and authorization and several classes for event handling, in such a way:
//loading game/lobby/other socket routers
...
var socketRouter = function(server) {
var io = Io(server);
io.use(socketioJwt.authorize({
secret: config.secretKey,
handshake: true
}));
io.on("connection", function(socket) {
lobbySocketRouter(socket);
gameSocketRouter(socket);
//other routers
...
socket.on("disconnect", function() {
console.log("DISCONNECTED", socket.id);
});
});
};
It's not a problem to generate unique event names for different routers to not interfere with each other. The problem is in disconnect event - I want every router having possibility to perform right action on it. Is it correct to add own handler to disconnect event in every router like this so each of them would trigger:
//lobbySocketRouter
socket.on("disconnect", function() {
//handling lobbySocketRouter special actions
});
...
//gameSocketRouter
socket.on("disconnect", function() {
//handling gameSocketRouter special actions
});
?