Skip to content

Commit

Permalink
feat: socket referenced in the data object.
Browse files Browse the repository at this point in the history
 - data.socket simplifyng its use and readability
  • Loading branch information
frankpagan committed Sep 17, 2023
1 parent efa3954 commit 1dbad0d
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SocketServer extends EventEmitter {
self.removeClient(socket)
});

this.send(socket, { method: 'connect', connectedKey: socket.config.key });
this.send(socket, { socket, method: 'connect', connectedKey: socket.config.key });

}

Expand All @@ -76,7 +76,7 @@ class SocketServer extends EventEmitter {
this.clients.set(key, clients);

if (user_id)
this.emit('userStatus', socket, { user_id, userStatus: 'on', organization_id });
this.emit('userStatus', socket, { socket, user_id, userStatus: 'on', organization_id });
}

removeClient(socket) {
Expand All @@ -91,7 +91,7 @@ class SocketServer extends EventEmitter {
}

if (user_id)
this.emit('userStatus', socket, { user_id, status: 'off', organization_id });
this.emit('userStatus', socket, { socket, user_id, status: 'off', organization_id });

if (clients.length == 0) {
this.organizations.delete(organization_id)
Expand Down Expand Up @@ -122,6 +122,8 @@ class SocketServer extends EventEmitter {
user_id = await this.authenticate.decodeToken(req);

if (this.authorize) {
data.socket = socket

data.host = this.getHost(req)
const authorized = await this.authorize.check(data, user_id)
if (authorized.storage === false) {
Expand All @@ -134,7 +136,7 @@ class SocketServer extends EventEmitter {
}
} else if (!authorized || authorized.error) {
if (!user_id)
this.send(socket, { method: 'updateUserStatus', userStatus: 'off', clientId: data.clientId, organization_id })
this.send(socket, { socket, method: 'updateUserStatus', userStatus: 'off', clientId: data.clientId, organization_id })

return this.send(socket, { method: 'Access Denied', authorized, ...data })
}
Expand All @@ -151,10 +153,10 @@ class SocketServer extends EventEmitter {
if (user_id) {
if (!socket.config.user_id) {
socket.config.user_id = user_id
this.emit('userStatus', socket, { method: 'userStatus', user_id, userStatus: 'on', organization_id });
this.emit('userStatus', socket, { socket, method: 'userStatus', user_id, userStatus: 'on', organization_id });
}
} else {
this.send(socket, { method: 'updateUserStatus', userStatus: 'off', clientId: data.clientId, organization_id })
this.send(socket, { socket, method: 'updateUserStatus', userStatus: 'off', clientId: data.clientId, organization_id })
}

this.emit(data.method, socket, data);
Expand Down Expand Up @@ -207,9 +209,8 @@ class SocketServer extends EventEmitter {
const authorized = await this.authorize.check(data, socket.config.user_id)
if (authorized && authorized.authorized)
data = authorized.authorized

delete data.socket
let responseData = JSON.stringify(data);

client.send(responseData);

if (socket.config && socket.config.organization_id)
Expand Down

0 comments on commit 1dbad0d

Please sign in to comment.