Answer the question
In order to leave comments, you need to log in
Is it possible to cluster socket.io multi-room chat?
Good day to all!
There is such a conditional app.js construct:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
...
app.get('/create', function(req, res) {
var id = Math.round((Math.random() * 1000000));
res.redirect('/room/' + id);
addRoom(id);
});
app.get('/room/:id', function(req, res) {
if (roomExists(req.params.id)) {
if (getRoomByID(req.params.id).usersinroom <= max_users_count) {
res.sendFile(__dirname + '/chatroom.html');
} else if (getRoomByID(req.params.id).usersinroom > max_users_count) {
req.flash('info', 'В комнате нет свободных мест')
res.redirect('/');
}
} else {
req.flash('info', 'Комнаты не существует')
res.redirect('/');
}
});
...
io.on('connection', function (socket) {
usersCount++;
id++;
socket.playerid = id;
var addedUser = false;
socket.on('initme', function(data) {
socket.join(data.roomID);
socket.roomID = data.roomID;
if (this.roomID != "lobby") {
getRoomByID(data.roomID).usersinroom++;
users = io.sockets.adapter.rooms[data.roomID].length;
var currentmap = maps.filter(function (obj) {
return obj.id == data.roomID;
})[0];
setTimeout(function () {
socket.emit('initial', { id: id});
}, 1000);
io.sockets.emit("entinroom", {"id": id, "room": data.roomID, "clients": users});
}
});
....
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question