Fix for empty rooms

This commit is contained in:
Alan Friedman 2016-01-06 21:48:49 -05:00
parent bfba7023ad
commit b90acce4cc
2 changed files with 9 additions and 10 deletions

View File

@ -38,22 +38,21 @@ app.use(express.static(__dirname + '/public'));
// Routes
function generateNewRoom(req, res, id) {
function generateNewRoom(id) {
const room = new Room(io, id);
rooms.push(room);
console.log('generating new room');
// room.on('empty', () => {
// console.log('room empty');
// rooms = _.without(rooms, _.findWhere(rooms, {id: room.id}));
// });
return res.render('index', {username: shortid.generate()});
room.on('empty', function() {
console.log('room empty');
rooms = _.without(rooms, _.findWhere(rooms, {_id: this._id}));
});
}
app.get('/', (req, res) => {
const id = shortid.generate();
generateNewRoom(id);
res.redirect(`/${id}`);
});
@ -62,7 +61,6 @@ app.get('/:roomId', (req, res) => {
let roomExists = false;
rooms.forEach( (room) => {
console.log(room, roomId);
if (room._id === roomId) {
roomExists = true;
}
@ -70,6 +68,8 @@ app.get('/:roomId', (req, res) => {
if (roomExists) {
return res.render('index', {username: shortid.generate()});
} else {
return res.sendStatus(404);
}
generateNewRoom(req, res, roomId);

View File

@ -54,7 +54,6 @@ class Room {
// when the user disconnects.. perform this
socket.on('disconnect', () => {
this.emit('empty');
if (addedUser) {
--this.numUsers;
@ -66,7 +65,7 @@ class Room {
// remove room from rooms array
if (this.numUsers === 0) {
this.emit('empty');
}
}
});