From 94d3af7b7e0fdfd56d00b8636e7444af45280474 Mon Sep 17 00:00:00 2001 From: hippoz Date: Tue, 17 Nov 2020 18:13:07 +0200 Subject: [PATCH] make gateway send debug info and add some egde case handling, like empty messages --- api/v1/gateway/index.js | 29 ++++++++++++++++++++++------- app/resources/js/app.js | 5 +++-- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/api/v1/gateway/index.js b/api/v1/gateway/index.js index ede156f..bbc7c2b 100644 --- a/api/v1/gateway/index.js +++ b/api/v1/gateway/index.js @@ -56,29 +56,43 @@ GatewayServer.prototype.eventSetup = function() { if (permissionLevel < config.roleMap.USER) return this.authDisconnect(socket, callback); - socket.username = data.username; - socket.userId = user._id; + socket.user = { + username: data.username, + _id: user._id + }; console.log(`[*] [gateway] [handshake] User ${data.username} has successfully authenticated`); return callback(); }); }); this._gateway.on('connection', (socket) => { - console.log(`[*] [gateway] [handshake] User ${socket.username} connected, sending hello and waiting for yoo...`); - socket.emit('hello'); + console.log(`[*] [gateway] [handshake] User ${socket.user.username} connected, sending hello and waiting for yoo...`); + + socket.emit('hello', { + gatewayStillNotConnectedTimeoutMS: config.gatewayStillNotConnectedTimeoutMS, + resolvedUser: { + username: socket.user.username, + _id: socket.user._id + } + }); + socket.once('yoo', () => { - console.log(`[*] [gateway] [handshake] Got yoo from ${socket.username}, connection is finally completed!`); + console.log(`[*] [gateway] [handshake] Got yoo from ${socket.user.username}, connection is finally completed!`); socket.isConnected = true; socket.on('message', ({ category, content }) => { + if (!category || !content) return; + content = content.trim(); + if (!content || content === '' || content === ' ') return; + // TODO: When/if category permissions are added, check if the user has permissions for that category const categoryTitle = socket.joinedCategories[category._id]; if (!categoryTitle) return; const messageObject = { author: { - username: socket.username, - _id: socket.userId + username: socket.user.username, + _id: socket.user._id }, category: { title: categoryTitle, @@ -93,6 +107,7 @@ GatewayServer.prototype.eventSetup = function() { }); socket.on('subscribe', async (categories) => { + if (!categories || !Array.isArray(categories) || categories === []) return; for (let v of categories) { // TODO: When/if category permissions are added, check if the user has permissions for that category const category = await Category.findById(v); diff --git a/app/resources/js/app.js b/app/resources/js/app.js index b55ffc6..e5acd6f 100755 --- a/app/resources/js/app.js +++ b/app/resources/js/app.js @@ -80,11 +80,12 @@ GatewayConnection.prototype.connect = function(token) { }); this.socket.on('connect', () => { - this.socket.once('hello', () => { - console.log('[*] [gateway] [handshake] Got hello from server, sending yoo...'); + this.socket.once('hello', (debugInfo) => { + console.log('[*] [gateway] [handshake] Got hello from server, sending yoo...', debugInfo); this.socket.emit('yoo'); this.isConnected = true; this.onConnect('CONNECT_RECEIVED_HELLO'); + console.log('[*] [gateway] [handshake] Assuming that server received yoo and that connection is completed.'); }); });