diff --git a/src/app.js b/src/app.js index 9121bb4..6b83c2d 100644 --- a/src/app.js +++ b/src/app.js @@ -7,6 +7,7 @@ import Io from 'socket.io'; import http from 'http'; import shortid from 'shortid'; import _ from 'underscore'; +import Room from './room'; const app = express(); const server = http.createServer(app); @@ -35,93 +36,33 @@ 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); + const room = new Room(io, id); rooms.push(room); + room.on('empty', () => { + rooms = _.without(rooms, _.findWhere(rooms, {id: room.id})); + }); + res.redirect(`/${id}`); }); -app.get('/*', (req, res) => { +app.get('/:roomId', (req, res) => { + + const roomId = req.param.roomId || false; + + rooms.forEach( (room) => { + console.log(room); + }) + res.render('index', {username: shortid.generate()}); }); +// Events + server.listen(3000, () => { console.log('fatty is on.'); }); diff --git a/src/room.js b/src/room.js new file mode 100644 index 0000000..2caf858 --- /dev/null +++ b/src/room.js @@ -0,0 +1,83 @@ +import _ from 'underscore'; +import {EventEmitter} from 'events'; +import util from 'util'; + +class Room { + constructor(io = {}, id = {}) { + this._id = id; + this.numUsers = 0; + EventEmitter.call(this); + + 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', () => { + this.emit('empty'); + 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) { + + } + } + }); + }); + } + + get roomId() { + return this.id; + } +} + +util.inherits(Room, EventEmitter); + +export default Room;