send offline message

This commit is contained in:
Peter Maquiran
2024-08-07 15:23:23 +01:00
parent b0a334c9dd
commit bbacc35b08
9 changed files with 172 additions and 4 deletions
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject, Subject, timer } from 'rxjs';
import { BehaviorSubject, Observable, Subject, timer } from 'rxjs';
import { Platform } from '@ionic/angular';
import { SignalRConnection } from './signalR';
import { Plugins } from '@capacitor/core';
@@ -102,6 +102,16 @@ export class SignalRService {
this.sendDataSubject.next(data)
this.deadConnectionBackGround.next()
})
this.connection.getConnectionState().subscribe((data) => {
this.connectingSubject.next(data)
})
} else {
setTimeout(() => {
this.establishConnection()
}, 2000)
}
}
@@ -131,6 +141,9 @@ export class SignalRService {
getData() {
return this.sendDataSubject.asObservable()
}
public getConnectionState(): Observable<boolean> {
return this.connectingSubject.asObservable();
}
async sendMessage(data: Object) {
return await this.connection.sendMessage(data as any)
+10 -2
View File
@@ -27,6 +27,7 @@ export class SignalRConnection {
private sendDataSubject: BehaviorSubject<{method: string, data: any}> = new BehaviorSubject<{method: string, data: any}>(null);
private pendingRequests: Map<string, { resolve: Function; reject: Function }> = new Map();
url: string
private hasConnectOnce = false
constructor({url}) {
this.url = url
@@ -44,6 +45,7 @@ export class SignalRConnection {
hubConnection
.start()
.then(() => {
this.hasConnectOnce = true
console.log('Connection started');
this.connectionStateSubject.next(true);
this.hubConnection = hubConnection
@@ -54,7 +56,13 @@ export class SignalRConnection {
.catch(error => {
console.log('Error while starting connection: ' + error);
this.connectionStateSubject.next(false);
reject(err(false))
if(this.hasConnectOnce) {
setTimeout(()=> {
this.attempReconnect();
}, 2000)
}
resolve(err(false))
});
hubConnection.onclose(() => {
@@ -64,7 +72,7 @@ export class SignalRConnection {
this.pendingRequests.forEach((_, requestId) => {
const { reject } = this.pendingRequests.get(requestId);
reject(err('Connection closed================!s'));
reject(err(false));
this.pendingRequests.delete(requestId);
});