-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlightningws.js
46 lines (43 loc) · 1.12 KB
/
lightningws.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
39
40
41
42
43
44
45
46
const WebSocket = require("ws").Server,
express = require("express"),
app = express(),
server = new WebSocket({
"server": app.listen(8040)
});
let object_id = null;
let subscriptions = [];
let ws = null;
server.on("connection", socket => {
ws = socket;
socket.on("message", message => {
var object = JSON.parse(message);
object_id = object.id;
subscriptions.push(object_id);
socket.send(JSON.stringify({
"pid": object.id,
"status": "opened"
}));
});
socket.onclose = function(e) {
if (object_id) {
var newarr = unsub(subscriptions, object_id);
subscriptions = newarr;
}
};
});
app.post("/", function(req, res) {
var post_string = req.headers.post,
post_object = JSON.parse(post_string),
inv_id = post_object.pid;
if (ws) {
if (subscriptions.includes(inv_id)) {
ws.send(post_string);
} else {
ws.send("no match");
}
}
res.sendStatus(200);
});
function unsub(subs, pid) {
return subs.filter(f => f !== pid);
}