Skip to content

Commit

Permalink
Send ping message down events gateway every 30seconds to keep it alive.
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartcaunt committed Nov 15, 2024
1 parent d6a64b9 commit 86061f9
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/app/core/services/events-gateway.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import {Inject, Injectable} from "@angular/core";
import {AccountService} from "./account.service";
import * as uuid from 'uuid';
import {environment} from "../../../environments/environment";
import {filter} from "rxjs/operators";
import {filter, takeUntil} from "rxjs/operators";
import {Store} from "@ngrx/store";
import {ApplicationState} from "../state";
import {selectLoggedInUser} from "../reducers";
import defaultOptions, {EventsGatewayConfig} from "./models/events-gateway-config.model";
import {timer} from "rxjs";
import {interval, Subject, timer} from "rxjs";
import {convertJsonDates} from "./models/json-date-converter";


Expand Down Expand Up @@ -85,24 +85,38 @@ export class EventsGateway {
this._reconnectionState = null;

this._socket = webSocket<GatewayEvent>(url);

const destroy$: Subject<boolean> = new Subject<boolean>();
interval(30000).pipe(
takeUntil(destroy$),
).subscribe({
next: (_) => this.emit('ping'),
});

const onSocketClose = () => {
if (this._socket) {
destroy$.next(true);
this._socket = null;
this._handleReconnection();
}
}

this._socket.subscribe({
next: (desktopEvent: GatewayEvent) => {
this._handleGatewayEvent(desktopEvent.type, desktopEvent.data);
},
error: (error) => {
if (error instanceof CloseEvent) {
console.error('Events Gateway closed: reconnecting...', error);
this._socket = null;
this._handleReconnection();
onSocketClose();

} else if (error instanceof Event) {
console.error(`Events Gateway error:`, error);
}
},
complete: () => {
this._socket = null;
console.log(`Received events gateway complete`);
this._handleReconnection();
onSocketClose();
}
});
},
Expand Down Expand Up @@ -130,7 +144,7 @@ export class EventsGateway {
this._subscribers = [];
}

emit(type: string, data: any) {
emit(type: string, data?: any) {
if (this._socket) {
this._socket.next({type, data});
}
Expand Down

0 comments on commit 86061f9

Please sign in to comment.