From bd1fbb09a4c64678e1b6b02f47c396d6c5165924 Mon Sep 17 00:00:00 2001 From: Daniel Seripap Date: Sun, 25 Dec 2016 22:57:57 -0500 Subject: [PATCH] Updated support for static chat rooms, added disconnected auto reload --- src/app.js | 11 ++++++++++- src/js/app.js | 37 ++++++++++++++++++++++++++++++++++--- src/js/fileHandler.js | 2 +- src/restricted.txt | 11 +++++++++++ src/room.js | 1 - 5 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 src/restricted.txt diff --git a/src/app.js b/src/app.js index 181e656..574d1f5 100644 --- a/src/app.js +++ b/src/app.js @@ -40,7 +40,16 @@ function generateNewRoom(req, res, id) { app.get('/', (req, res) => generateNewRoom(req, res, 'lobby') ); app.get('/:roomId', (req, res) => { - const roomId = req.params.roomId || false; + const stripName = (name) => { + const chatName = name.toLowerCase().replace(/[^A-Za-z0-9]/g, '-'); + if (chatName.length >= 25) { + return chatName.substr(0, 25); + } + + return chatName; + }; + + const roomId = stripName(req.params.roomId) || false; let roomExists = _.findWhere(rooms, {_id: roomId}) || false; diff --git a/src/js/app.js b/src/js/app.js index 6f01c24..1b3eb29 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -9,14 +9,26 @@ import he from 'he'; export default class App { constructor() { - this._roomId = window.location.pathname.length ? window.location.pathname : null; + this._roomId = window.location.pathname.length ? this.stripName(window.location.pathname) : null; this._darkwire = new Darkwire(); this._socket = io(this._roomId); - this._chat = new Chat(this._darkwire, this._socket); + this._darkwire.connected = false; this.init(); } + stripName(name) { + const chatName = name.replace('/','').toLowerCase().replace(/[^A-Za-z0-9]/g, '-'); + if (chatName.length >= 25) { + const limitedChatName = chatName.substr(0, 25); + window.history.replaceState( {} , limitedChatName, `/${limitedChatName}` ); + return `/${limitedChatName}`; + } + + return '/' + chatName; + }; + init() { + this._chat = new Chat(this._darkwire, this._socket); if (!this._roomId) { return; } @@ -104,6 +116,14 @@ export default class App { this._chat.removeChatTyping(data); }); + this._socket.on('disconnect', (data) => { + this._darkwire.connected = false; + this._chat.log('Disconnected from server, automatically reloading chatroom in 10 seconds.', { + error: true, + }); + this.retryConnection(); + }); + this.initChat(); // Nav links @@ -249,8 +269,13 @@ export default class App { addParticipantsMessage(data) { let message = ''; let headerMsg = ''; + const { numUsers } = data; - $('#participants').text(data.numUsers); + if (numUsers === 0) { + window.location.reload(); + } + + $('#participants').text(numUsers); } renderParticipantsList() { @@ -268,4 +293,10 @@ export default class App { }); } + retryConnection() { + window.setTimeout(() => { + window.location.reload(); + }, 10000); + } + } diff --git a/src/js/fileHandler.js b/src/js/fileHandler.js index 48e0508..185c35a 100644 --- a/src/js/fileHandler.js +++ b/src/js/fileHandler.js @@ -25,7 +25,7 @@ export default class FileHandler { } confirmTransfer(event) { - const validFileTypes = ['png','jpg','jpeg','gif','zip','rar','gzip','pdf','txt','json','doc','docx']; + const validFileTypes = ['png','jpg','jpeg','gif','zip','rar','gzip','pdf','txt','json','doc','docx','csv','js','html','css']; const file = event.target.files && event.target.files[0]; if (file) { diff --git a/src/restricted.txt b/src/restricted.txt new file mode 100644 index 0000000..e077731 --- /dev/null +++ b/src/restricted.txt @@ -0,0 +1,11 @@ +admin +admins +about +about-us +darkwire +official +l0bby +10bby +1obby +i0bby +iobby diff --git a/src/room.js b/src/room.js index e4a950a..acd0455 100644 --- a/src/room.js +++ b/src/room.js @@ -70,7 +70,6 @@ class Room { // when the user disconnects.. perform this socket.on('disconnect', () => { - console.log('disconnected'); if (addedUser) { --this.numUsers; this.users = _.without(this.users, socket.user);