forked from hippoz/brainlet
make gateway send debug info and add some egde case handling, like empty messages
This commit is contained in:
parent
68e2bd1a6c
commit
94d3af7b7e
2 changed files with 25 additions and 9 deletions
|
@ -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);
|
||||
|
|
|
@ -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.');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue