Skip to content

Commit

Permalink
fix: Delete AsyncMessage.js as by design the results are async & refa…
Browse files Browse the repository at this point in the history
…ctor index.js Emitting; authorized data in send method

The commit deletes AsyncMessage.js and changes the index.js file by removing the import statement and the creation of an instance of AsyncMessage. Refactoring was done on the emit method. The send method was also refactored to include authorized data.
  • Loading branch information
frankpagan committed May 25, 2023
1 parent 870c9ab commit b47e6fc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 135 deletions.
52 changes: 0 additions & 52 deletions src/AsyncMessage.js

This file was deleted.

111 changes: 28 additions & 83 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
const WebSocket = require('ws');
const url = require("url");
const EventEmitter = require("events").EventEmitter;
const AsyncMessage = require("./AsyncMessage")
const uid = require('@cocreate/uuid')

class SocketServer extends EventEmitter {
constructor(prefix) {
super();

this.clients = new Map();
this.asyncMessages = new Map();

this.prefix = prefix || "crud";

//. websocket server
this.wss = new WebSocket.Server({ noServer: true });
}

Expand Down Expand Up @@ -63,12 +57,6 @@ class SocketServer extends EventEmitter {
room_clients = [socket];
}
this.clients.set(key, room_clients);
// this.addAsyncMessage(key)

let asyncMessage = this.asyncMessages.get(key)
if (!asyncMessage) {
this.asyncMessages.set(key, new AsyncMessage(key));
}

if (user_id)
this.emit('userStatus', socket, { user_id, userStatus: 'on', organization_id });
Expand Down Expand Up @@ -99,10 +87,10 @@ class SocketServer extends EventEmitter {
if (user_id)
this.emit('userStatus', socket, { user_id, status: 'off', organization_id });

// TODO: remove if no one else connected to organization
this.emit("deleteMetrics", { organization_id });
this.emit("deletePermissions", organization_id);
this.emit('disconnect', organization_id)
this.asyncMessages.delete(key);
} else {
let total_cnt = 0;
this.clients.forEach((c) => total_cnt += c.length)
Expand Down Expand Up @@ -167,16 +155,6 @@ class SocketServer extends EventEmitter {
this.send(socket, 'updateUserStatus', { userStatus: 'off', clientId: data.clientId, organization_id })
}

//. checking async status....
if (data.async == true) {
console.log('async true')
const asyncMessage = this.asyncMessages.get(socket.config.key);
socket.config.asyncId = uid.generate();
if (asyncMessage) {
asyncMessage.defineMessage(socket.config.asyncId);
}
}

this.emit(action, socket, data);

}
Expand All @@ -190,13 +168,6 @@ class SocketServer extends EventEmitter {
if (!data.uid)
data.uid = uid.generate()

const asyncId = socket.config.asyncId
let isAsync = false;
let asyncData = [];
if (asyncId && socket.config && socket.config.key) {
isAsync = true;
}

let organization_id = socket.config.organization_id;
let url = `/${this.prefix}/${organization_id}`;

Expand All @@ -214,72 +185,46 @@ class SocketServer extends EventEmitter {
let url = url;
url += `/${room}`;

this.send(socket, action, data, url, isAsync, asyncData)
this.send(socket, action, data, url)
}
} else {
this.send(socket, action, data, url, isAsync, asyncData)
this.send(socket, action, data, url)
}
}

if (isAsync) {
this.asyncMessages.get(socket.config.key).setMessage(asyncId, asyncData)
}
async send(socket, action, data, url) {
let clients
if (!url)
clients = [socket]
else
clients = this.clients.get(url);

if (clients) {
for (let client of clients) {
if (socket != client && data.broadcast != false || socket == client && data.broadcastSender != false) {
const permission = await this.authorize.check(action, data, socket.config.user_id)
if (permission && permission.authorized)
data = permission.authorized

}
let responseData = JSON.stringify({
action,
data
});

async send(socket, action, data, url, isAsync, asyncData) {
const asyncId = socket.config.asyncId
socket.send(responseData);

if (!url && asyncId && socket.config && socket.config.key) {
let responseData = JSON.stringify({
action,
data
});
this.asyncMessages.get(socket.config.key).setMessage(asyncId, [{ socket, message: responseData }]);
} else {
let clients
if (!url)
clients = [socket]
else
clients = this.clients.get(url);

if (clients) {
for (let client of clients) {
if (socket != client && data.broadcast != false || socket == client && data.broadcastSender != false) {
if (isAsync) {
asyncData.push({ socket, message: JSON.stringify({ action, data }) })
} else {
const permission = await this.authorize.check(action, data, socket.config.user_id)
if (permission && permission.authorized)
data = permission.authorized

let responseData = JSON.stringify({
action,
data
});

socket.send(responseData);

if (socket.config && socket.config.organization_id)
this.emit("setBandwidth", {
type: 'out',
data: responseData,
organization_id: socket.config.organization_id
})
}
}
if (socket.config && socket.config.organization_id)
this.emit("setBandwidth", {
type: 'out',
data: responseData,
organization_id: socket.config.organization_id
})
}
}
}

}

// addAsyncMessage(key) {
// let asyncMessage = this.asyncMessages.get(key)
// if (!asyncMessage) {
// this.asyncMessages.set(key, new AsyncMessage(key));
// }
// }

getKeyFromUrl(pathname) {
var path = pathname.split("/");
var params = {
Expand Down

0 comments on commit b47e6fc

Please sign in to comment.