@nka212bg/backend-utils 0.1.19 → 0.1.20
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/package.json +1 -1
- package/socket.js +18 -51
package/package.json
CHANGED
package/socket.js
CHANGED
|
@@ -1,40 +1,29 @@
|
|
|
1
1
|
const WebSocket = require("ws");
|
|
2
|
-
let http = null;
|
|
3
|
-
let https = null;
|
|
4
2
|
let socketsCount = 0;
|
|
3
|
+
let sockets = [];
|
|
5
4
|
|
|
6
|
-
exports.socket = (
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
} else if (httpsServer) {
|
|
13
|
-
https = new WebSocket.Server({ server: httpsServer });
|
|
14
|
-
https.on("connection", (ws, req) => {
|
|
15
|
-
ws.params = JSON.parse(`{"${decodeURI(req.url.replace(/\/\?/g, "")).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"')}"}`);
|
|
16
|
-
});
|
|
17
|
-
}
|
|
5
|
+
exports.socket = (server) => {
|
|
6
|
+
const sl = sockets.length;
|
|
7
|
+
sockets[sl] = new WebSocket.Server({ server });
|
|
8
|
+
sockets[sl].on("connection", (ws, req) => {
|
|
9
|
+
ws.params = JSON.parse(`{"${decodeURI(req.url.replace(/\/\?/g, "")).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"')}"}`);
|
|
10
|
+
});
|
|
18
11
|
};
|
|
19
12
|
|
|
20
13
|
exports.getUsersStatuses = (users) => {
|
|
21
14
|
if (typeof users == "string") users = users.split(/[, ]/g);
|
|
22
|
-
if (!Array.isArray(users) || !
|
|
15
|
+
if (!Array.isArray(users) || !sockets.length) return {};
|
|
23
16
|
|
|
24
17
|
const usersObj = {};
|
|
25
18
|
users.forEach((u) => {
|
|
26
19
|
usersObj[u] = { online: false };
|
|
27
|
-
for (const c of https.clients) {
|
|
28
|
-
if (c.params.userId == u) {
|
|
29
|
-
usersObj[u] = { online: true };
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
20
|
|
|
34
|
-
for (const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
21
|
+
for (const socket of sockets) {
|
|
22
|
+
for (const client of socket.clients) {
|
|
23
|
+
if (client.params.userId == u) {
|
|
24
|
+
usersObj[u] = { online: true };
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
38
27
|
}
|
|
39
28
|
}
|
|
40
29
|
});
|
|
@@ -42,34 +31,11 @@ exports.getUsersStatuses = (users) => {
|
|
|
42
31
|
return usersObj;
|
|
43
32
|
};
|
|
44
33
|
|
|
45
|
-
// exports.getUsersStatuses = (users = []) => {
|
|
46
|
-
// users = users.map((u) => {
|
|
47
|
-
// u = { id: u, online: false };
|
|
48
|
-
// for (const c of https.clients) {
|
|
49
|
-
// if (c.params.userId == u.id) {
|
|
50
|
-
// u.online = true;
|
|
51
|
-
// return u;
|
|
52
|
-
// }
|
|
53
|
-
// }
|
|
54
|
-
|
|
55
|
-
// for (const c of http.clients) {
|
|
56
|
-
// if (c.params.userId == u.id) {
|
|
57
|
-
// u.online = true;
|
|
58
|
-
// return u;
|
|
59
|
-
// }
|
|
60
|
-
// }
|
|
61
|
-
|
|
62
|
-
// return u;
|
|
63
|
-
// });
|
|
64
|
-
|
|
65
|
-
// return users;
|
|
66
|
-
// };
|
|
67
|
-
|
|
68
34
|
exports.sendSocketMessage = ({ userId, skipId, skipToken, data, broadcast } = {}) => {
|
|
69
35
|
let sentCount = 0;
|
|
70
36
|
let tempCount = 0;
|
|
71
37
|
|
|
72
|
-
if (!userId) return;
|
|
38
|
+
if ((!userId && !broadcast) || !sockets.length) return;
|
|
73
39
|
userId = !Array.isArray(userId) ? [String(userId)] : userId.map((e) => String(e));
|
|
74
40
|
|
|
75
41
|
if (skipId) skipId = !Array.isArray(skipId) ? [String(skipId)] : skipId.map((e) => String(e));
|
|
@@ -79,8 +45,9 @@ exports.sendSocketMessage = ({ userId, skipId, skipToken, data, broadcast } = {}
|
|
|
79
45
|
if (typeof data != "string") data = JSON.stringify(data);
|
|
80
46
|
} catch {}
|
|
81
47
|
|
|
82
|
-
|
|
83
|
-
|
|
48
|
+
sockets.forEach((socket) => {
|
|
49
|
+
(socket.clients || []).forEach(manageClient);
|
|
50
|
+
});
|
|
84
51
|
|
|
85
52
|
socketsCount = tempCount;
|
|
86
53
|
return sentCount;
|