From ee4ac056bfe3fae4de9155854bb0045d5eaec854 Mon Sep 17 00:00:00 2001 From: Alan Friedman Date: Wed, 6 Jan 2016 18:31:00 -0500 Subject: [PATCH] Add support for multiple rooms --- src/app.js | 139 ++++++++++++++++++++++++++------------------- src/public/main.js | 6 +- 2 files changed, 86 insertions(+), 59 deletions(-) diff --git a/src/app.js b/src/app.js index ed9f4f6..9121bb4 100644 --- a/src/app.js +++ b/src/app.js @@ -6,6 +6,7 @@ import Redis from 'connect-redis'; import Io from 'socket.io'; import http from 'http'; import shortid from 'shortid'; +import _ from 'underscore'; const app = express(); const server = http.createServer(app); @@ -22,7 +23,7 @@ const sessionMiddleware = session({ saveUninitialized: true }); -let numUsers = 0; +var rooms = []; io.use(function(socket, next) { sessionMiddleware(socket.request, socket.request.res, next); @@ -34,69 +35,91 @@ app.set('views', __dirname + '/views'); app.use(sessionMiddleware); app.use(express.static(__dirname + '/public')); +class Room { + constructor(id) { + this.id = id; + this.numUsers = 0; + this.init(); + } + init() { + const thisIO = io.of(this.id); + + thisIO.on('connection', (socket) => { + let addedUser = false; + + // when the client emits 'new message', this listens and executes + socket.on('new message', (data) => { + // we tell the client to execute 'new message' + socket.broadcast.emit('new message', { + username: socket.username, + message: data + }); + }); + + socket.on('add user', (username) => { + if (addedUser) return; + + // we store the username in the socket session for this client + socket.username = username; + ++this.numUsers; + addedUser = true; + socket.emit('login', { + numUsers: this.numUsers + }); + // echo globally (all clients) that a person has connected + socket.broadcast.emit('user joined', { + username: socket.username, + numUsers: this.numUsers + }); + }); + + // when the client emits 'typing', we broadcast it to others + socket.on('typing', () => { + socket.broadcast.emit('typing', { + username: socket.username + }); + }); + + // when the client emits 'stop typing', we broadcast it to others + socket.on('stop typing', () => { + socket.broadcast.emit('stop typing', { + username: socket.username + }); + }); + + // when the user disconnects.. perform this + socket.on('disconnect', () => { + if (addedUser) { + --this.numUsers; + + // echo globally that this client has left + socket.broadcast.emit('user left', { + username: socket.username, + numUsers: this.numUsers + }); + + // remove room from rooms array + if (this.numUsers === 0) { + rooms = _.without(rooms, _.findWhere(rooms, {id: this.id})); + } + } + }); + }); + } +} + // Routes app.get('/', (req, res) => { + const id = shortid.generate(); + const room = new Room(id); + rooms.push(room); - res.render('index', {username: shortid.generate()}); + res.redirect(`/${id}`); }); - -io.on('connection', (socket) => { - let addedUser = false; - - // when the client emits 'new message', this listens and executes - socket.on('new message', (data) => { - // we tell the client to execute 'new message' - socket.broadcast.emit('new message', { - username: socket.username, - message: data - }); - }); - - socket.on('add user', (username) => { - if (addedUser) return; - - // we store the username in the socket session for this client - socket.username = username; - ++numUsers; - addedUser = true; - socket.emit('login', { - numUsers: numUsers - }); - // echo globally (all clients) that a person has connected - socket.broadcast.emit('user joined', { - username: socket.username, - numUsers: numUsers - }); - }); - - // when the client emits 'typing', we broadcast it to others - socket.on('typing', () => { - socket.broadcast.emit('typing', { - username: socket.username - }); - }); - - // when the client emits 'stop typing', we broadcast it to others - socket.on('stop typing', () => { - socket.broadcast.emit('stop typing', { - username: socket.username - }); - }); - - // when the user disconnects.. perform this - socket.on('disconnect', () => { - if (addedUser) { - --numUsers; - - // echo globally that this client has left - socket.broadcast.emit('user left', { - username: socket.username, - numUsers: numUsers - }); - } - }); +app.get('/*', (req, res) => { + res.render('index', {username: shortid.generate()}); }); server.listen(3000, () => { diff --git a/src/public/main.js b/src/public/main.js index e2ec492..6468930 100644 --- a/src/public/main.js +++ b/src/public/main.js @@ -22,7 +22,11 @@ $(function() { var lastTypingTime; var $currentInput = $usernameInput.focus(); - var socket = io(); + var roomId = window.location.pathname.length ? window.location.pathname : null; + + if (!roomId) return; + + var socket = io(roomId); function addParticipantsMessage (data) { var message = '';