Add support for multiple rooms

This commit is contained in:
Alan Friedman 2016-01-06 18:31:00 -05:00
parent b727a046c0
commit ee4ac056bf
2 changed files with 86 additions and 59 deletions

View File

@ -6,6 +6,7 @@ import Redis from 'connect-redis';
import Io from 'socket.io'; import Io from 'socket.io';
import http from 'http'; import http from 'http';
import shortid from 'shortid'; import shortid from 'shortid';
import _ from 'underscore';
const app = express(); const app = express();
const server = http.createServer(app); const server = http.createServer(app);
@ -22,7 +23,7 @@ const sessionMiddleware = session({
saveUninitialized: true saveUninitialized: true
}); });
let numUsers = 0; var rooms = [];
io.use(function(socket, next) { io.use(function(socket, next) {
sessionMiddleware(socket.request, socket.request.res, next); sessionMiddleware(socket.request, socket.request.res, next);
@ -34,69 +35,91 @@ app.set('views', __dirname + '/views');
app.use(sessionMiddleware); app.use(sessionMiddleware);
app.use(express.static(__dirname + '/public')); 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 // Routes
app.get('/', (req, res) => { 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}`);
}); });
app.get('/*', (req, res) => {
io.on('connection', (socket) => { res.render('index', {username: shortid.generate()});
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
});
}
});
}); });
server.listen(3000, () => { server.listen(3000, () => {

View File

@ -22,7 +22,11 @@ $(function() {
var lastTypingTime; var lastTypingTime;
var $currentInput = $usernameInput.focus(); 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) { function addParticipantsMessage (data) {
var message = ''; var message = '';