-
Notifications
You must be signed in to change notification settings - Fork 4
/
websocket.js
38 lines (32 loc) · 906 Bytes
/
websocket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const { server } = require("./app");
const { WebSocketServer } = require("ws");
const pathMatch = require("path-match")({
sensitive: false,
strict: false,
end: true
});
const { URL } = require("url");
let matchRoute = pathMatch("/ws/subscribers/:aeTitle");
WebSocketServer.prototype.shouldHandle = function (req) {
let params = matchRoute(req.url);
if (params) {
req.params = params;
return true;
}
return false;
};
const ws = new WebSocketServer({ server });
ws.on("connection", function (retWs, req) {
let { aeTitle } = req.params;
retWs.aeTitle = aeTitle;
});
/**
*
* @param {string} aeTitle
* @returns {import("ws").WebSocket[]}
*/
function findWsArrayByAeTitle(aeTitle) {
let wsArray = [...ws.clients];
return wsArray.filter(wsClient => wsClient.aeTitle === aeTitle);
}
module.exports.findWsArrayByAeTitle = findWsArrayByAeTitle;