Isolated room.js

This commit is contained in:
Dan Seripap 2016-01-06 20:51:01 -05:00
parent ee4ac056bf
commit f377b0dc87
2 changed files with 99 additions and 75 deletions

View File

@ -7,6 +7,7 @@ import Io from 'socket.io';
import http from 'http'; import http from 'http';
import shortid from 'shortid'; import shortid from 'shortid';
import _ from 'underscore'; import _ from 'underscore';
import Room from './room';
const app = express(); const app = express();
const server = http.createServer(app); const server = http.createServer(app);
@ -35,93 +36,33 @@ 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 id = shortid.generate();
const room = new Room(id); const room = new Room(io, id);
rooms.push(room); rooms.push(room);
room.on('empty', () => {
rooms = _.without(rooms, _.findWhere(rooms, {id: room.id}));
});
res.redirect(`/${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()}); res.render('index', {username: shortid.generate()});
}); });
// Events
server.listen(3000, () => { server.listen(3000, () => {
console.log('fatty is on.'); console.log('fatty is on.');
}); });

83
src/room.js Normal file
View File

@ -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;