Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/snapcontrol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ class SnapControl {
this.msg_id = 0;
this.status_req_id = -1;
this.timer = null;
this.reconnection_attempts = 0;
}

public connect(baseUrl: string) {
Expand All @@ -269,6 +270,7 @@ class SnapControl {
this.connection = new WebSocket(baseUrl + '/jsonrpc');
this.connection.onmessage = (msg: MessageEvent) => this.onMessage(msg.data);
this.connection.onopen = () => {
this.reconnection_attempts = 0;
this.status_req_id = this.sendRequest('Server.GetStatus');
if (this.onConnectionChanged)
this.onConnectionChanged(this, true);
Expand All @@ -277,17 +279,22 @@ class SnapControl {
this.connection.onclose = () => {
if (this.onConnectionChanged)
this.onConnectionChanged(this, false, 'Connection lost, trying to reconnect.');
console.info('connection lost, reconnecting in 1s');
this.timer = setTimeout(() => this.connect(baseUrl), 1000);
const reconnectionDelay = this.getReconnectDelay();
this.timer = setTimeout(() => this.connect(baseUrl), reconnectionDelay * 1000);
};
} catch (e) {
console.info('Exception while connecting: "' + e + '", reconnecting in 1s');
const reconnectionDelay = this.getReconnectDelay();
console.info('Exception while connecting: "' + e + '", reconnecting in ' + reconnectionDelay + 's');
if (this.onConnectionChanged)
this.onConnectionChanged(this, false, 'Exception while connecting: "' + e + '", trying to reconnect.');
this.timer = setTimeout(() => this.connect(baseUrl), 1000);
this.timer = setTimeout(() => this.connect(baseUrl), reconnectionDelay * 1000);
}
}

private getReconnectDelay() {
return Math.min(1.1 ** this.reconnection_attempts++ - 1, 1);
}

public disconnect() {
if (this.timer)
clearTimeout(this.timer);
Expand All @@ -298,6 +305,7 @@ class SnapControl {
}
}
if (this.onConnectionChanged)

this.onConnectionChanged(this, false);
}

Expand Down Expand Up @@ -532,6 +540,7 @@ class SnapControl {
msg_id: number;
status_req_id: number;
timer: ReturnType<typeof setTimeout> | null;
reconnection_attempts: number
}


Expand Down