This commit is contained in:
Alan Friedman 2017-11-17 06:44:01 -05:00
parent 3a9cf86ea2
commit d637bd6194
3 changed files with 23 additions and 24 deletions

View File

@ -44,8 +44,7 @@ function stripName(name) {
}
return chatName;
};
}
app.get('/', (req, res) => generateNewRoom(req, res, stripName(shortid.generate())));

View File

@ -265,7 +265,7 @@ export default class App {
}
this._chat.log(moment().format('MMMM Do YYYY, h:mm:ss a'), {info: true});
$('#roomName').text(this._roomId);
$('#chatNameModal').text(this._roomId);

View File

@ -1,7 +1,7 @@
import _ from "underscore";
import { EventEmitter } from "events";
import util from "util";
import uuid from "uuid";
import _ from 'underscore';
import {EventEmitter} from 'events';
import util from 'util';
import uuid from 'uuid';
class Room {
constructor(io = {}, id = {}) {
@ -15,20 +15,20 @@ class Room {
const thisIO = io.of(this._id);
thisIO.on("connection", socket => {
thisIO.on('connection', socket => {
let addedUser = false;
// when the client emits 'new message', this listens and executes
socket.on("new message", data => {
const { username } = socket;
socket.on('new message', data => {
const {username} = socket;
const isRateLimited = this.isRateLimited(username);
if (isRateLimited) {
return thisIO.emit("rated", { username, id: socket.user.id });
return thisIO.emit('rated', {username, id: socket.user.id});
}
// we tell the client to execute 'new message'
socket.broadcast.emit("new message", {
socket.broadcast.emit('new message', {
username,
id: socket.user.id,
message: data.message,
@ -40,7 +40,7 @@ class Room {
});
});
socket.on("add user", data => {
socket.on('add user', data => {
if (addedUser) {
return;
}
@ -58,7 +58,7 @@ class Room {
addedUser = true;
// Broadcast to ALL sockets, including this one
thisIO.emit("user joined", {
thisIO.emit('user joined', {
username: socket.username,
numUsers: this.numUsers,
users: this.users
@ -66,23 +66,23 @@ class Room {
});
// when the client emits 'typing', we broadcast it to others
socket.on("typing", () => {
socket.broadcast.emit("typing", { username: socket.username });
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 });
socket.on('stop typing', () => {
socket.broadcast.emit('stop typing', {username: socket.username});
});
// when the user disconnects.. perform this
socket.on("disconnect", () => {
socket.on('disconnect', () => {
if (addedUser) {
--this.numUsers;
this.users = _.without(this.users, socket.user);
// echo globally that this client has left
socket.broadcast.emit("user left", {
socket.broadcast.emit('user left', {
username: socket.username,
numUsers: this.numUsers,
users: this.users,
@ -91,13 +91,13 @@ class Room {
// remove room from rooms array
if (this.numUsers === 0) {
this.emit("empty");
this.emit('empty');
}
}
});
// Update user
socket.on("update user", data => {
socket.on('update user', data => {
const newUsername = this.sanitizeUsername(data.newUsername);
if (newUsername.length > 16) {
@ -112,7 +112,7 @@ class Room {
socket.username = user.username = newUsername;
socket.user = user;
thisIO.emit("user update", {
thisIO.emit('user update', {
username: socket.username,
id: socket.user.id
});
@ -151,7 +151,7 @@ class Room {
}
sanitizeUsername(str) {
return str.replace(/[^A-Za-z0-9]/g, "-");
return str.replace(/[^A-Za-z0-9]/g, '-');
}
roomId() {