aboutsummaryrefslogtreecommitdiffstats
path: root/lib/sockets.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sockets.js')
-rw-r--r--lib/sockets.js26
1 files changed, 18 insertions, 8 deletions
diff --git a/lib/sockets.js b/lib/sockets.js
index 514d701a..117a8d67 100644
--- a/lib/sockets.js
+++ b/lib/sockets.js
@@ -6,10 +6,21 @@ wss = new WebSocket.Server({ clientTracking: false, noServer: true });
/* eslint-disable-next-line */
const pick = (O, ...K) => K.reduce((o, k) => ((o[k] = O[k]), o), {});
+const subscriptions = [];
emit = (username, type, data) => {
if (data.username !== undefined) data = pick(data, ...whitelist);
+ if (type === "payment" && data.amount > 0) {
+ for (let i = subscriptions.length - 1; i >= 0; i--) {
+ let s = subscriptions[i];
+ if (s.invoice.amount === data.amount) {
+ s.ws.send(JSON.stringify({ type, data }));
+ subscriptions.splice(i, 1);
+ }
+ }
+ }
+
if (!sockets[username]) return;
for (id in sockets[username]) {
const ws = sockets[username][id];
@@ -46,18 +57,17 @@ wss.on("connection", function (ws, req) {
l.error("coudn't parse socket message");
}
- let token;
switch (type) {
case "login":
- token = data;
+ let token = data;
if (!token || token === "null") return;
try {
const username = jwt.decode(token).username;
if (!sockets[username]) sockets[username] = {};
const existing = Object.keys(sockets[username]);
if (existing.length > 5) {
- l.warn("too many sockets, logging out", existing[5]);
- sockets[username][existing[5]].send(
+ l.warn("too many sockets, logging out", existing[3]);
+ sockets[username][existing[3]].send(
JSON.stringify({ type: "login", data: false })
);
}
@@ -88,7 +98,6 @@ wss.on("connection", function (ws, req) {
ws.send(
JSON.stringify({ type: "version", data: config.clientVersion })
);
- l.info("socket open", username, id);
} catch (e) {
ws.close();
l.error(
@@ -101,9 +110,8 @@ wss.on("connection", function (ws, req) {
}
break;
case "token":
- token = data;
if (!ws.user) return;
- ws.token = token;
+ ws.token = data;
break;
case "lnurl":
sessions[data] = ws;
@@ -125,6 +133,9 @@ wss.on("connection", function (ws, req) {
emit(ws.user.username, "payment", payment);
}
break;
+ case "subscribe":
+ subscriptions.push({ invoice: data, ws });
+ break;
default:
l.warn("received socket message of unknown type", type, data);
}
@@ -135,7 +146,6 @@ wss.on("connection", function (ws, req) {
const username = names[id];
if (sockets[username] && sockets[username][id])
delete sockets[username][id];
- l.info("socket closed", username, id);
} catch (e) {
l.error("problem closing socket", e.message);
}