fix: signal disconnect loop on server start

this fixes the disconnect loop when the server has just started by
waiting for the connection to open before subsribing to events.

also includes other improvements:
- rename types to interfaces
- allow to input single receiver for signal
- use discovery api to check for upgrade path instead hard coded one

Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
Heikki Hellgren
2024-01-26 09:31:06 +02:00
parent 9a8d556661
commit 447d21045b
17 changed files with 89 additions and 46 deletions
+14 -14
View File
@@ -146,20 +146,6 @@ export class SignalClient implements SignalApi {
url.protocol = url.protocol === 'http:' ? 'ws:' : 'wss:';
this.ws = new WebSocket(url.toString(), token);
this.ws.onmessage = (data: MessageEvent) => {
this.handleMessage(data);
};
this.ws.onerror = () => {
this.reconnect();
};
this.ws.onclose = (ev: CloseEvent) => {
if (ev.code !== WS_CLOSE_NORMAL && ev.code !== WS_CLOSE_GOING_AWAY) {
this.reconnect();
}
};
// Wait until connection is open
let connectSleep = 0;
while (
@@ -174,6 +160,20 @@ export class SignalClient implements SignalApi {
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
throw new Error('Connect timeout');
}
this.ws.onmessage = (data: MessageEvent) => {
this.handleMessage(data);
};
this.ws.onerror = () => {
this.reconnect();
};
this.ws.onclose = (ev: CloseEvent) => {
if (ev.code !== WS_CLOSE_NORMAL && ev.code !== WS_CLOSE_GOING_AWAY) {
this.reconnect();
}
};
}
private handleMessage(data: MessageEvent) {