Initial commit

This commit is contained in:
2026-02-18 23:47:03 +01:00
commit 94757e8cc3
8 changed files with 421 additions and 0 deletions

41
static/subscription.mjs Normal file
View File

@@ -0,0 +1,41 @@
export class Subscription_Endpoint extends EventTarget {
async connect(path) {
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = `${wsProtocol}//${location.host}${path}`;
const ws = new WebSocket(wsUrl);
await new Promise((resolve, reject) => {
ws.addEventListener('open', resolve, { once: true });
ws.addEventListener('error', reject, { once: true });
});
ws.addEventListener('message', ev => {
this.dispatchEvent(new CustomEvent('message', { detail: ev.data }));
});
ws.addEventListener('close', () => {
this.dispatchEvent(new Event('close'));
});
ws.addEventListener('error', err => {
this.dispatchEvent(new CustomEvent('error', { detail: err }));
});
this.ws = ws;
return ws;
}
subscribe(resource) {
const { ws } = this;
ws.send(JSON.stringify({
cmd: 'subscribe',
resource
}));
}
}