Browse Source

style: tidied up Socket.io code

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 5 years ago
parent
commit
b701244fb9
2 changed files with 77 additions and 71 deletions
  1. 31 20
      backend/logic/io.js
  2. 46 51
      frontend/io.js

+ 31 - 20
backend/logic/io.js

@@ -15,7 +15,7 @@ module.exports = class extends coreClass {
 	}
 
 	initialize() {
-		return new Promise((resolve, reject) => {
+		return new Promise(resolve => {
 			this.setStage(1);
 
 			const 	logger		= this.logger,
@@ -27,21 +27,22 @@ module.exports = class extends coreClass {
 			
 			const actions = require('../logic/actions');
 
-			//TODO Check every 30s/60s, for all sockets, if they are still allowed to be in the rooms they are in, and on socket at all (permission changing/banning)
+			// TODO: Check every 30s/60s, for all sockets, if they are still allowed to be in the rooms they are in, and on socket at all (permission changing/banning)
 			this.io = socketio(app.server);
 
 			this.io.use(async (socket, next) => {
 				try { await this._validateHook(); } catch { return; }
 
-				let cookies = socket.request.headers.cookie;
 				let SID;
 
 				socket.ip = socket.request.headers['x-forwarded-for'] || '0.0.0.0';
 
 				async.waterfall([
 					(next) => {
-						utils.parseCookies(cookies).then((parsedCookies) => {
-							SID = parsedCookies.SID;
+						utils.parseCookies(
+							socket.request.headers.cookie
+						).then(res => {
+							SID = res.SID;
 							next(null);
 						});
 					},
@@ -50,35 +51,43 @@ module.exports = class extends coreClass {
 						if (!SID) return next('No SID.');
 						next();
 					},
+
 					(next) => {
 						cache.hget('sessions', SID, next);
 					},
+
 					(session, next) => {
 						if (!session) return next('No session found.');
+
 						session.refreshDate = Date.now();
+						
 						socket.session = session;
 						cache.hset('sessions', SID, session, next);
 					},
+
 					(res, next) => {
+						// check if a session's user / IP is banned
 						punishments.getPunishments((err, punishments) => {
 							const isLoggedIn = !!(socket.session && socket.session.refreshDate);
 							const userId = (isLoggedIn) ? socket.session.userId : null;
-							let ban = 0;
-							let banned = false;
+
+							let banishment = { banned: false, ban: 0 };
+
 							punishments.forEach(punishment => {
-								if (punishment.expiresAt > ban) ban = punishment;
-								if (punishment.type === 'banUserId' && isLoggedIn && punishment.value === userId) banned = true;
-								if (punishment.type === 'banUserIp' && punishment.value === socket.ip) banned = true;
+								if (punishment.expiresAt > banishment.ban) banishment.ban = punishment;
+								if (punishment.type === 'banUserId' && isLoggedIn && punishment.value === userId) banishment.banned = true;
+								if (punishment.type === 'banUserIp' && punishment.value === socket.ip) banishment.banned = true;
 							});
-							socket.banned = banned;
-							socket.ban = ban;
+							
+							socket = { ...socket, ...banishment }
+
 							next();
 						});
 					}
 				], () => {
-					if (!socket.session) {
-						socket.session = { socketId: socket.id };
-					} else socket.session.socketId = socket.id;
+					if (!socket.session) socket.session = { socketId: socket.id };
+					else socket.session.socketId = socket.id;
+
 					next();
 				});
 			});
@@ -87,7 +96,10 @@ module.exports = class extends coreClass {
 				try { await this._validateHook(); } catch { return; }
 
 				let sessionInfo = '';
+				
 				if (socket.session.sessionId) sessionInfo = ` UserID: ${socket.session.userId}.`;
+
+				// if session is banned
 				if (socket.banned) {
 					logger.info('IO_BANNED_CONNECTION', `A user tried to connect, but is currently banned. IP: ${socket.ip}.${sessionInfo}`);
 					socket.emit('keep.event:banned', socket.ban);
@@ -96,18 +108,17 @@ module.exports = class extends coreClass {
 					logger.info('IO_CONNECTION', `User connected. IP: ${socket.ip}.${sessionInfo}`);
 
 					// catch when the socket has been disconnected
-					socket.on('disconnect', (reason) => {
-						let sessionInfo = '';
+					socket.on('disconnect', () => {
 						if (socket.session.sessionId) sessionInfo = ` UserID: ${socket.session.userId}.`;
 						logger.info('IO_DISCONNECTION', `User disconnected. IP: ${socket.ip}.${sessionInfo}`);
 					});
 
 					// catch errors on the socket (internal to socket.io)
-					socket.on('error', err => console.error(err));
+					socket.on('error', console.error);
 
 					// have the socket listen for each action
-					Object.keys(actions).forEach((namespace) => {
-						Object.keys(actions[namespace]).forEach((action) => {
+					Object.keys(actions).forEach(namespace => {
+						Object.keys(actions[namespace]).forEach(action => {
 
 							// the full name of the action
 							let name = `${namespace}.${action}`;

+ 46 - 51
frontend/io.js

@@ -1,47 +1,54 @@
-let callbacks = [];
-let callbacksPersist = [];
-let onConnectCallbacks = [];
-let onDisconnectCallbacks = [];
-let onConnectErrorCallbacks = [];
-const onConnectCallbacksPersist = [];
-const onDisconnectCallbacksPersist = [];
-const onConnectErrorCallbacksPersist = [];
+const callbacks = {
+	general: {
+		temp: [],
+		persist: []
+	},
+	onConnect: {
+		temp: [],
+		persist: []
+	},
+	onDisconnect: {
+		temp: [],
+		persist: []
+	},
+	onConnectError: {
+		temp: [],
+		persist: []
+	}
+};
 
 export default {
 	ready: false,
+
 	socket: null,
 
 	getSocket(...args) {
 		if (args[0] === true) {
 			if (this.ready) args[1](this.socket);
-			else callbacksPersist.push(args[1]);
+			else callbacks.general.persist.push(args[1]);
 		} else if (this.ready) args[0](this.socket);
-		else callbacks.push(args[0]);
+		else callbacks.general.temp.push(args[0]);
 	},
 
 	onConnect(...args) {
-		if (args[0] === true) {
-			onConnectCallbacksPersist.push(args[1]);
-		} else onConnectCallbacks.push(args[0]);
+		if (args[0] === true) callbacks.onConnect.persist.push(args[1]);
+		else callbacks.onConnect.temp.push(args[0]);
 	},
 
 	onDisconnect(...args) {
-		if (args[0] === true) {
-			onDisconnectCallbacksPersist.push(args[1]);
-		} else onDisconnectCallbacks.push(args[0]);
+		if (args[0] === true) callbacks.onDisconnect.persist.push(args[1]);
+		else callbacks.onDisconnect.temp.push(args[0]);
 	},
 
 	onConnectError(...args) {
-		if (args[0] === true) {
-			onConnectErrorCallbacksPersist.push(args[1]);
-		} else onConnectErrorCallbacks.push(args[0]);
+		if (args[0] === true) callbacks.onDisconnect.persist.push(args[1]);
+		else callbacks.onConnectError.temp.push(args[0]);
 	},
 
 	clear: () => {
-		onConnectCallbacks = [];
-		onDisconnectCallbacks = [];
-		onConnectErrorCallbacks = [];
-		callbacks = [];
+		Object.keys(callbacks).forEach(type => {
+			callbacks[type].temp = [];
+		});
 	},
 
 	removeAllListeners() {
@@ -49,50 +56,38 @@ export default {
 			if (
 				id.indexOf("$event:") !== -1 &&
 				id.indexOf("$event:keep.") === -1
-			) {
+			)
 				delete this.socket._callbacks[id];
-			}
 		});
 	},
 
 	init(url) {
 		/* eslint-disable-next-line no-undef */
 		this.socket = window.socket = io(url);
+
 		this.socket.on("connect", () => {
-			onConnectCallbacks.forEach(cb => {
-				cb();
-			});
-			onConnectCallbacksPersist.forEach(cb => {
-				cb();
-			});
+			callbacks.onConnect.temp.forEach(cb => cb());
+			callbacks.onConnect.persist.forEach(cb => cb());
 		});
+
 		this.socket.on("disconnect", () => {
 			console.log("IO: SOCKET DISCONNECTED");
-			onDisconnectCallbacks.forEach(cb => {
-				cb();
-			});
-			onDisconnectCallbacksPersist.forEach(cb => {
-				cb();
-			});
+			callbacks.onDisconnect.temp.forEach(cb => cb());
+			callbacks.onDisconnect.persist.forEach(cb => cb());
 		});
+
 		this.socket.on("connect_error", () => {
 			console.log("IO: SOCKET CONNECT ERROR");
-			onConnectErrorCallbacks.forEach(cb => {
-				cb();
-			});
-			onConnectErrorCallbacksPersist.forEach(cb => {
-				cb();
-			});
+			callbacks.onConnectError.temp.forEach(cb => cb());
+			callbacks.onConnectError.persist.forEach(cb => cb());
 		});
+
 		this.ready = true;
-		callbacks.forEach(callback => {
-			callback(this.socket);
-		});
 
-		callbacksPersist.forEach(callback => {
-			callback(this.socket);
-		});
-		callbacks = [];
-		callbacksPersist = [];
+		callbacks.general.temp.forEach(callback => callback(this.socket));
+		callbacks.general.persist.forEach(callback => callback(this.socket));
+
+		callbacks.general.temp = [];
+		callbacks.general.persist = [];
 	}
 };