@cocreate/socket-server 1.16.3 → 1.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/src/index.js +31 -31
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [1.17.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.16.3...v1.17.0) (2023-09-07)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* organzations map to store the orgs active status, if the org balance falls bellow 0 false is set resulting in socket and file-server responding with 0 balance error message ([e06b8b1](https://github.com/CoCreate-app/CoCreate-socket-server/commit/e06b8b173fc24b98b60a9bd24494bb1fceea19cc))
|
|
7
|
+
|
|
1
8
|
## [1.16.3](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.16.2...v1.16.3) (2023-08-21)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const WebSocket = require('ws');
|
|
2
|
-
const
|
|
2
|
+
const { URL } = require("url");
|
|
3
3
|
const EventEmitter = require("events").EventEmitter;
|
|
4
4
|
const uid = require('@cocreate/uuid')
|
|
5
5
|
const config = require('@cocreate/config')
|
|
@@ -7,8 +7,9 @@ const config = require('@cocreate/config')
|
|
|
7
7
|
class SocketServer extends EventEmitter {
|
|
8
8
|
constructor(server, prefix) {
|
|
9
9
|
super();
|
|
10
|
+
this.organizations = new Map();
|
|
10
11
|
this.clients = new Map();
|
|
11
|
-
this.prefix = prefix || "
|
|
12
|
+
this.prefix = prefix || "ws";
|
|
12
13
|
config({ organization_id: { prompt: 'Enter your organization_id: ' } })
|
|
13
14
|
|
|
14
15
|
this.wss = new WebSocket.Server({ noServer: true });
|
|
@@ -25,10 +26,11 @@ class SocketServer extends EventEmitter {
|
|
|
25
26
|
|
|
26
27
|
handleUpgrade(req, socket, head) {
|
|
27
28
|
const self = this;
|
|
28
|
-
const
|
|
29
|
-
const
|
|
29
|
+
// const url = new URL(req.url);
|
|
30
|
+
// const pathname = req.url;
|
|
31
|
+
const config = this.getKeyFromUrl(req.url)
|
|
30
32
|
if (config.type == this.prefix) {
|
|
31
|
-
|
|
33
|
+
this.wss.handleUpgrade(req, socket, head, function (socket) {
|
|
32
34
|
socket.config = config
|
|
33
35
|
self.onWebSocket(req, socket);
|
|
34
36
|
})
|
|
@@ -60,6 +62,9 @@ class SocketServer extends EventEmitter {
|
|
|
60
62
|
|
|
61
63
|
addClient(socket) {
|
|
62
64
|
let organization_id = socket.config.organization_id
|
|
65
|
+
if (!this.organizations.has(organization_id))
|
|
66
|
+
this.organizations.set(organization_id, true)
|
|
67
|
+
|
|
63
68
|
let user_id = socket.config.user_id
|
|
64
69
|
let key = socket.config.key
|
|
65
70
|
let clients = this.clients.get(key);
|
|
@@ -72,11 +77,6 @@ class SocketServer extends EventEmitter {
|
|
|
72
77
|
|
|
73
78
|
if (user_id)
|
|
74
79
|
this.emit('userStatus', socket, { user_id, userStatus: 'on', organization_id });
|
|
75
|
-
|
|
76
|
-
this.emit("createMetrics", {
|
|
77
|
-
organization_id,
|
|
78
|
-
clients: clients.length,
|
|
79
|
-
});
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
removeClient(socket) {
|
|
@@ -90,19 +90,12 @@ class SocketServer extends EventEmitter {
|
|
|
90
90
|
clients.splice(index, 1);
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
this.emit('userStatus', socket, { user_id, status: 'off', organization_id });
|
|
93
|
+
if (user_id)
|
|
94
|
+
this.emit('userStatus', socket, { user_id, status: 'off', organization_id });
|
|
96
95
|
|
|
97
|
-
|
|
98
|
-
this.
|
|
99
|
-
this.emit("deleteAuthorized", organization_id);
|
|
96
|
+
if (clients.length == 0) {
|
|
97
|
+
this.organizations.delete(organization_id)
|
|
100
98
|
this.emit('disconnect', organization_id)
|
|
101
|
-
} else {
|
|
102
|
-
this.emit("updateMetrics", {
|
|
103
|
-
organization_id,
|
|
104
|
-
clients: clients.length
|
|
105
|
-
});
|
|
106
99
|
}
|
|
107
100
|
|
|
108
101
|
}
|
|
@@ -110,14 +103,20 @@ class SocketServer extends EventEmitter {
|
|
|
110
103
|
async onMessage(req, socket, message) {
|
|
111
104
|
try {
|
|
112
105
|
const organization_id = socket.config.organization_id
|
|
106
|
+
|
|
107
|
+
this.emit("setBandwidth", {
|
|
108
|
+
type: 'in',
|
|
109
|
+
data: message,
|
|
110
|
+
organization_id
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
let active = this.organizations.get(organization_id)
|
|
114
|
+
if (active === false)
|
|
115
|
+
return this.send(socket, { method: 'Access Denied', balance: 'Your balance has fallen bellow 0' })
|
|
116
|
+
|
|
113
117
|
let data = JSON.parse(message)
|
|
114
118
|
|
|
115
119
|
if (data.method) {
|
|
116
|
-
this.emit("setBandwidth", {
|
|
117
|
-
type: 'in',
|
|
118
|
-
data: message,
|
|
119
|
-
organization_id
|
|
120
|
-
});
|
|
121
120
|
let user_id = null;
|
|
122
121
|
if (this.authenticate)
|
|
123
122
|
user_id = await this.authenticate.decodeToken(req);
|
|
@@ -239,13 +238,14 @@ class SocketServer extends EventEmitter {
|
|
|
239
238
|
}
|
|
240
239
|
|
|
241
240
|
getHost(req) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
241
|
+
if (req.headers.origin && req.headers.origin !== 'null') {
|
|
242
|
+
const url = new URL(req.headers.origin);
|
|
243
|
+
return url.host;
|
|
244
|
+
} else if (req.headers.host && req.headers.host !== 'null') {
|
|
245
|
+
return req.headers.host;
|
|
246
|
+
}
|
|
246
247
|
}
|
|
247
248
|
|
|
248
249
|
}
|
|
249
250
|
|
|
250
|
-
|
|
251
251
|
module.exports = SocketServer
|