Fix endless reconnections after a late WhoAmI

This commit is contained in:
FinnStutzenstein 2019-03-29 10:27:49 +01:00
parent 8f393a1877
commit 042fe29ffc

View File

@ -90,9 +90,10 @@ export class WebsocketService {
private subjects: { [type: string]: Subject<any> } = {}; private subjects: { [type: string]: Subject<any> } = {};
/** /**
* Saves, if the service is in retry mode to get a connection to a previos connection lost. * Saves, if the WS Connection should be closed (e.g. after an explicit `close()`). Prohibits
* retry connection attempts.
*/ */
private retry = false; private shouldBeClosed = true;
/** /**
* Counter for delaying the offline message. * Counter for delaying the offline message.
@ -122,12 +123,17 @@ export class WebsocketService {
options: { options: {
changeId?: number; changeId?: number;
enableAutoupdates?: boolean; enableAutoupdates?: boolean;
} = {} } = {},
retry: boolean = false
): void { ): void {
if (this.websocket) { if (this.websocket) {
this.close(); this.close();
} }
if (!retry) {
this.shouldBeClosed = false;
}
// set defaults // set defaults
options = Object.assign(options, { options = Object.assign(options, {
enableAutoupdates: true enableAutoupdates: true
@ -153,12 +159,14 @@ export class WebsocketService {
this.websocket.onopen = (event: Event) => { this.websocket.onopen = (event: Event) => {
this.zone.run(() => { this.zone.run(() => {
this.retryCounter = 0; this.retryCounter = 0;
if (this.retry) {
if (this.connectionErrorNotice) { if (this.shouldBeClosed) {
this.connectionErrorNotice.dismiss(); this.dismissConnectionErrorNotice();
this.connectionErrorNotice = null; return;
} }
this.retry = false;
if (retry) {
this.dismissConnectionErrorNotice();
this._reconnectEvent.emit(); this._reconnectEvent.emit();
} }
this._connectEvent.emit(); this._connectEvent.emit();
@ -190,8 +198,8 @@ export class WebsocketService {
this.websocket = null; this.websocket = null;
this._connectionOpen = false; this._connectionOpen = false;
// 1000 is a normal close, like the close on logout // 1000 is a normal close, like the close on logout
if (event.code !== 1000) { this._closeEvent.emit();
console.error(event); if (!this.shouldBeClosed && event.code !== 1000) {
// Do not show the message snackbar on the projector // Do not show the message snackbar on the projector
// tests for /projector and /projector/<id> // tests for /projector and /projector/<id>
const onProjector = this.router.url.match(/^\/projector(\/[0-9]+\/?)?$/); const onProjector = this.router.url.match(/^\/projector(\/[0-9]+\/?)?$/);
@ -211,11 +219,9 @@ export class WebsocketService {
// A random retry timeout between 2000 and 5000 ms. // A random retry timeout between 2000 and 5000 ms.
const timeout = Math.floor(Math.random() * 3000 + 2000); const timeout = Math.floor(Math.random() * 3000 + 2000);
setTimeout(() => { setTimeout(() => {
this.retry = true; this.connect({ enableAutoupdates: true }, true);
this.connect({ enableAutoupdates: true });
}, timeout); }, timeout);
} }
this._closeEvent.emit();
}); });
}; };
@ -228,10 +234,19 @@ export class WebsocketService {
}; };
} }
private dismissConnectionErrorNotice(): void {
if (this.connectionErrorNotice) {
this.connectionErrorNotice.dismiss();
this.connectionErrorNotice = null;
}
}
/** /**
* Closes the websocket connection. * Closes the websocket connection.
*/ */
public close(): void { public close(): void {
this.shouldBeClosed = true;
this.dismissConnectionErrorNotice();
if (this.websocket) { if (this.websocket) {
this.websocket.close(); this.websocket.close();
this.websocket = null; this.websocket = null;