From 893b1d4ff672fd4535ced91b4b11141ab806443e Mon Sep 17 00:00:00 2001 From: hippoz Date: Mon, 16 Nov 2020 22:08:23 +0200 Subject: [PATCH] add message sending --- api/v1/gateway/index.js | 27 ++++++++++++++++++++++----- app/resources/js/app.js | 28 +++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/api/v1/gateway/index.js b/api/v1/gateway/index.js index f043a7c..350e983 100644 --- a/api/v1/gateway/index.js +++ b/api/v1/gateway/index.js @@ -14,6 +14,7 @@ class GatewayServer { } GatewayServer.prototype.authDisconnect = function(socket, callback) { + console.log('[E] [gateway] [handshake] User disconnected due to failed authentication'); socket.isConnected = false; socket.disconnect(); socket.disconnect(true); @@ -22,12 +23,12 @@ GatewayServer.prototype.authDisconnect = function(socket, callback) { GatewayServer.prototype.eventSetup = function() { this._gateway.use((socket, callback) => { - console.log('[*] [gateway] User authentication attempt'); + console.log('[*] [gateway] [handshake] User authentication attempt'); socket.isConnected = false; setTimeout(() => { if (socket.isConnected) return; - console.log('[*] [gateway] User still not connected after timeout, removing...'); + console.log('[*] [gateway] [handshake] User still not connected after timeout, removing...'); socket.disconnect(); socket.disconnect(true); }, config.gatewayStillNotConnectedTimeoutMS); @@ -54,17 +55,33 @@ GatewayServer.prototype.eventSetup = function() { if (permissionLevel < config.roleMap.USER) return this.authDisconnect(socket, callback); socket.username = data.username; - console.log(`[*] [gateway] User ${data.username} has successfully authenticated`); + console.log(`[*] [gateway] [handshake] User ${data.username} has successfully authenticated`); return callback(); }); }); this._gateway.on('connection', (socket) => { - console.log(`[*] [gateway] User ${socket.username} connected, sending hello and waiting for yoo...`); + console.log(`[*] [gateway] [handshake] User ${socket.username} connected, sending hello and waiting for yoo...`); socket.emit('hello'); socket.once('yoo', () => { - console.log(`[*] [gateway] Got yoo from ${socket.username}, connection is finally completed!`); + console.log(`[*] [gateway] [handshake] Got yoo from ${socket.username}, connection is finally completed!`); socket.isConnected = true; + + socket.on('message', ({ categoryId, content }) => { + // TODO: URGENT: Check if the category exists and if the user has access to it (access coming soon) + socket.to(categoryId).emit('message', { + username: socket.username, + categoryId: categoryId, + content: content + }); + }); + + socket.on('subscribe', (categories) => { + for (let v of categories) { + // TODO: URGENT: Check if the category exists and if the user has access to it (access coming soon) + socket.join(v); + } + }); }); }); }; diff --git a/app/resources/js/app.js b/app/resources/js/app.js index 6d60a21..39ad208 100755 --- a/app/resources/js/app.js +++ b/app/resources/js/app.js @@ -76,6 +76,7 @@ GatewayConnection.prototype.connect = function(token) { this.socket.on('hello', () => { console.log('[*] [gateway] [handshake] Got hello from server, sending yoo...'); this.socket.emit('yoo'); + this.isConnected = true; this.onConnect('CONNECT_RECEIVED_HELLO'); }); @@ -96,6 +97,19 @@ GatewayConnection.prototype.connect = function(token) { }); }; +GatewayConnection.prototype.sendMessage = function(categoryId, content) { + if (!this.isConnected) return; + + this.socket.emit('message', { categoryId, content }); +}; + +GatewayConnection.prototype.subscribeToCategoryChat = function(categoryId) { + if (!this.isConnected) return; + + this.socket.emit('subscribe', [categoryId]); +}; + + const app = new Vue({ el: '#app', data: { @@ -204,8 +218,17 @@ const app = new Vue({ this.resetSnackbarButton(); this.notification('ERROR: You have been disconnected from the gateway. Realtime features such as chat will not work and unexpected errors may occur.'); }; + this.gateway.onConnect = () => { + this.gateway.socket.on('message', ({ username, categoryId, content }) => { + this.showMessageNotification(username, content); + }) + }; this.gateway.connect(this.loggedInUser.token); }, + showMessageNotification: async function(username, content) { + this.resetSnackbarButton(); + this.notification(`${username}: ${content}`); + }, button: function(text, click) { this.cardButtons.push({ text, click }); }, @@ -397,7 +420,10 @@ const app = new Vue({ this.cardButtons = []; this.button('Chat', (post) => { - this.browse(post); + if (post._id) { + this.gateway.subscribeToCategoryChat(post._id); + this.gateway.sendMessage(post._id, 'yoooo'); + } }); this.button('View', (post) => { this.browse(post);