make gateway send debug info and add some egde case handling, like empty messages

This commit is contained in:
hippoz 2020-11-17 18:13:07 +02:00
parent 68e2bd1a6c
commit 94d3af7b7e
2 changed files with 25 additions and 9 deletions

View file

@ -56,29 +56,43 @@ GatewayServer.prototype.eventSetup = function() {
if (permissionLevel < config.roleMap.USER) return this.authDisconnect(socket, callback); if (permissionLevel < config.roleMap.USER) return this.authDisconnect(socket, callback);
socket.username = data.username; socket.user = {
socket.userId = user._id; username: data.username,
_id: user._id
};
console.log(`[*] [gateway] [handshake] User ${data.username} has successfully authenticated`); console.log(`[*] [gateway] [handshake] User ${data.username} has successfully authenticated`);
return callback(); return callback();
}); });
}); });
this._gateway.on('connection', (socket) => { this._gateway.on('connection', (socket) => {
console.log(`[*] [gateway] [handshake] User ${socket.username} connected, sending hello and waiting for yoo...`); console.log(`[*] [gateway] [handshake] User ${socket.user.username} connected, sending hello and waiting for yoo...`);
socket.emit('hello');
socket.emit('hello', {
gatewayStillNotConnectedTimeoutMS: config.gatewayStillNotConnectedTimeoutMS,
resolvedUser: {
username: socket.user.username,
_id: socket.user._id
}
});
socket.once('yoo', () => { 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.isConnected = true;
socket.on('message', ({ category, content }) => { 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 // TODO: When/if category permissions are added, check if the user has permissions for that category
const categoryTitle = socket.joinedCategories[category._id]; const categoryTitle = socket.joinedCategories[category._id];
if (!categoryTitle) return; if (!categoryTitle) return;
const messageObject = { const messageObject = {
author: { author: {
username: socket.username, username: socket.user.username,
_id: socket.userId _id: socket.user._id
}, },
category: { category: {
title: categoryTitle, title: categoryTitle,
@ -93,6 +107,7 @@ GatewayServer.prototype.eventSetup = function() {
}); });
socket.on('subscribe', async (categories) => { socket.on('subscribe', async (categories) => {
if (!categories || !Array.isArray(categories) || categories === []) return;
for (let v of categories) { for (let v of categories) {
// TODO: When/if category permissions are added, check if the user has permissions for that category // TODO: When/if category permissions are added, check if the user has permissions for that category
const category = await Category.findById(v); const category = await Category.findById(v);

View file

@ -80,11 +80,12 @@ GatewayConnection.prototype.connect = function(token) {
}); });
this.socket.on('connect', () => { this.socket.on('connect', () => {
this.socket.once('hello', () => { this.socket.once('hello', (debugInfo) => {
console.log('[*] [gateway] [handshake] Got hello from server, sending yoo...'); console.log('[*] [gateway] [handshake] Got hello from server, sending yoo...', debugInfo);
this.socket.emit('yoo'); this.socket.emit('yoo');
this.isConnected = true; this.isConnected = true;
this.onConnect('CONNECT_RECEIVED_HELLO'); this.onConnect('CONNECT_RECEIVED_HELLO');
console.log('[*] [gateway] [handshake] Assuming that server received yoo and that connection is completed.');
}); });
}); });