rentgendroid/android/code/notifications.mjs
Andrii Dokhniak ca973c4114 Notifications
2025-04-09 22:40:35 +02:00

37 lines
906 B
JavaScript

import { WebSocket } from "ws";
import { WebSocketServer } from "ws";
const notification_proxy = new WebSocketServer({ port: 3001 });
let notification_subs = [];
notification_proxy.on('connection', (ws) => {
notification_subs.push(ws);
});
export function send_notification(is_ok, context, message)
{
let updated_subs = [];
if (notification_subs.length === 0) {
console.log("WARNING: Got a notification, but nobody is subscribed");
}
for (const sub of notification_subs) {
if (sub.readyState == WebSocket.CONNECTING) {
console.log("WARNING: Unable to forward a notification to client that is still connecting");
updated_subs.push(sub);
} else {
try {
sub.send(JSON.stringify({
is_ok,
context,
message
}));
updated_subs.push(sub);
} catch {
sub.close();
console.log("WARNING: Fail to send a notification, closing the connection");
}
}
}
}