mirror of
https://github.com/darkwire/darkwire.io.git
synced 2025-07-18 18:54:52 +00:00
Add support for multiple rooms
This commit is contained in:
parent
b727a046c0
commit
ee4ac056bf
51
src/app.js
51
src/app.js
@ -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,15 +35,16 @@ app.set('views', __dirname + '/views');
|
|||||||
app.use(sessionMiddleware);
|
app.use(sessionMiddleware);
|
||||||
app.use(express.static(__dirname + '/public'));
|
app.use(express.static(__dirname + '/public'));
|
||||||
|
|
||||||
// Routes
|
class Room {
|
||||||
|
constructor(id) {
|
||||||
|
this.id = id;
|
||||||
|
this.numUsers = 0;
|
||||||
|
this.init();
|
||||||
|
}
|
||||||
|
init() {
|
||||||
|
const thisIO = io.of(this.id);
|
||||||
|
|
||||||
app.get('/', (req, res) => {
|
thisIO.on('connection', (socket) => {
|
||||||
|
|
||||||
res.render('index', {username: shortid.generate()});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
io.on('connection', (socket) => {
|
|
||||||
let addedUser = false;
|
let addedUser = false;
|
||||||
|
|
||||||
// when the client emits 'new message', this listens and executes
|
// when the client emits 'new message', this listens and executes
|
||||||
@ -59,15 +61,15 @@ io.on('connection', (socket) => {
|
|||||||
|
|
||||||
// we store the username in the socket session for this client
|
// we store the username in the socket session for this client
|
||||||
socket.username = username;
|
socket.username = username;
|
||||||
++numUsers;
|
++this.numUsers;
|
||||||
addedUser = true;
|
addedUser = true;
|
||||||
socket.emit('login', {
|
socket.emit('login', {
|
||||||
numUsers: numUsers
|
numUsers: this.numUsers
|
||||||
});
|
});
|
||||||
// echo globally (all clients) that a person has connected
|
// echo globally (all clients) that a person has connected
|
||||||
socket.broadcast.emit('user joined', {
|
socket.broadcast.emit('user joined', {
|
||||||
username: socket.username,
|
username: socket.username,
|
||||||
numUsers: numUsers
|
numUsers: this.numUsers
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -88,16 +90,37 @@ io.on('connection', (socket) => {
|
|||||||
// when the user disconnects.. perform this
|
// when the user disconnects.. perform this
|
||||||
socket.on('disconnect', () => {
|
socket.on('disconnect', () => {
|
||||||
if (addedUser) {
|
if (addedUser) {
|
||||||
--numUsers;
|
--this.numUsers;
|
||||||
|
|
||||||
// echo globally that this client has left
|
// echo globally that this client has left
|
||||||
socket.broadcast.emit('user left', {
|
socket.broadcast.emit('user left', {
|
||||||
username: socket.username,
|
username: socket.username,
|
||||||
numUsers: numUsers
|
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.redirect(`/${id}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get('/*', (req, res) => {
|
||||||
|
res.render('index', {username: shortid.generate()});
|
||||||
|
});
|
||||||
|
|
||||||
server.listen(3000, () => {
|
server.listen(3000, () => {
|
||||||
console.log('fatty is on.');
|
console.log('fatty is on.');
|
||||||
|
@ -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 = '';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user