add message sending
This commit is contained in:
parent
dcdcc64553
commit
893b1d4ff6
2 changed files with 49 additions and 6 deletions
|
@ -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);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
|
|
Reference in a new issue