This commit is contained in:
Peter Maquiran
2023-07-12 10:53:32 +01:00
7 changed files with 114 additions and 37 deletions
+3 -14
View File
@@ -10,7 +10,6 @@ import { ThemeService } from 'src/app/services/theme.service';
import { environment } from 'src/environments/environment';
import { Storage } from '@ionic/storage';
import { ChatController } from './controller/chat';
import { AngularFireMessaging } from '@angular/fire/messaging';
const CUSTOM_DATE_FORMATS: NgxMatDateFormats = {
parse: {
@@ -41,10 +40,11 @@ export class AppComponent {
private storage: Storage,
private ChatSystemService: ChatSystemService
) {
this.initializeApp();
this.storage.set('version', environment.version).then(() => {})
ChatController.ChatSystemService = this.ChatSystemService
}
/* requestPermission() {
@@ -69,24 +69,13 @@ export class AppComponent {
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
/* this.splashScreen.hide(); */
if (this.platform.is("tablet")) {
window.screen.orientation.unlock();
} else if( this.platform.is("mobile")) {
window.screen.orientation.lock('portrait');
}
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
} else {
try {
// this.sqliteservice.databaseConn();
} catch (error) {
}
}
});
}
}
+3 -1
View File
@@ -7,7 +7,7 @@ import { StatusBar } from '@ionic-native/status-bar/ngx';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
import { InAppBrowser } from '@ionic-native/in-app-browser/ngx';
@@ -81,6 +81,7 @@ import { AngularFireModule } from '@angular/fire';
import { AngularFireMessagingModule } from '@angular/fire/messaging';
import { firebaseConfig } from '../firebase-config';
import { EditorModule } from '@tinymce/tinymce-angular';
import { LoggingInterceptorService } from './services/logging-interceptor.service';
// import { ServiceWorkerModule } from '@angular/service-worker';
// import { AngularFireModule } from '@angular/fire';
@@ -199,6 +200,7 @@ import { FirebaseX } from '@ionic-native/firebase-x/ngx'; */
NgxExtendedPdfViewerModule,
FileOpener,
DocumentViewer,
{ provide: HTTP_INTERCEPTORS, useClass: LoggingInterceptorService, multi: true },
],
bootstrap: [AppComponent],
+30 -15
View File
@@ -21,7 +21,14 @@ export class BackgroundService {
private themeservice: ThemeService,
private storageservice: StorageService,
private http: HttpClient,
) { }
) {
window.addEventListener('focus', (event) => {
if(this.status == 'offline') {
this.tryToReachTheServer()
}
});
}
online() {
if(this.status == 'online') {
@@ -68,28 +75,36 @@ export class BackgroundService {
return false
}
const hasReachedTheServer = await this.tryToReachTheServer()
if(!hasReachedTheServer) {
this.status = 'offline'
document.body.style.setProperty(`--color`, "#ffb703");
document.body.style.setProperty(`--color2`, "#ffb703");
document.body.style.setProperty(`--color3`, "#ffb703");
document.body.style.setProperty(`--color4`, "#ffb703");
document.body.style.setProperty(`--color5`, "#ffb703");
this.storageservice.store('networkCheckStore','offline');
this.callBacks.forEach((e) => {
if (e.type == 'Offline') {
e.funx()
}
})
}
}
async tryToReachTheServer() {
let opts = {
headers: {},
}
try {
await this.http.post(environment.apiURL + "UserAuthentication/Login", '', opts).toPromise();
return true
} catch (error) {
if(error.status != 400) {
this.status = 'offline'
document.body.style.setProperty(`--color`, "#ffb703");
document.body.style.setProperty(`--color2`, "#ffb703");
document.body.style.setProperty(`--color3`, "#ffb703");
document.body.style.setProperty(`--color4`, "#ffb703");
document.body.style.setProperty(`--color5`, "#ffb703");
this.storageservice.store('networkCheckStore','offline');
this.callBacks.forEach((e) => {
if (e.type == 'Offline') {
e.funx()
}
})
return false
}
}
}
@@ -5,6 +5,7 @@ import { deepFind } from 'src/plugin/deep'
import { environment } from 'src/environments/environment';
import { SessionStore } from 'src/app/store/session.service';
import { chatHistory, Rooms } from 'src/app/models/chatMethod';
import { BackgroundService } from '../background.service';
@Injectable({
providedIn: 'root'
@@ -14,7 +15,7 @@ export class RochetChatConnectorService {
isLogin = false;
loginResponse = {}
constructor() {
constructor(private backgroundservice: BackgroundService,) {
}
connect() {
@@ -752,6 +753,7 @@ export class RochetChatConnectorService {
},
onopen: async ()=> {
this.ws.connected = true
this.backgroundservice.online();
setTimeout(()=>{
this.ws.wsMsgQueue()
@@ -804,6 +806,9 @@ export class RochetChatConnectorService {
},
onmessage: async (event: any)=> {
this.backgroundservice.online();
const data = JSON.parse(event.data)
for (const [key, value] of Object.entries(this.wsCallbacks)) {
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { LoggingInterceptorService } from './logging-interceptor.service';
describe('LoggingInterceptorService', () => {
let service: LoggingInterceptorService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(LoggingInterceptorService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -0,0 +1,50 @@
import { HttpHandler, HttpRequest, HttpResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { finalize, tap } from 'rxjs/operators';
import { BackgroundService } from './background.service';
@Injectable({
providedIn: 'root'
})
export class LoggingInterceptorService {
constructor(
private backgroundservice: BackgroundService,
) {}
intercept(req: HttpRequest<any>, next: HttpHandler) {
const started = Date.now();
let ok: string;
// extend server response observable with logging
return next.handle(req)
.pipe(
tap({
// Succeeds when there is a response; ignore other events
next: (event) => {
ok = event instanceof HttpResponse ? 'succeeded' : ''
if(ok == 'succeeded' || (typeof event['status'] == 'number' && event['status'] !=0)) {
// set to online
this.backgroundservice.online();
}
},
// Operation failed; error is an HttpErrorResponse
error: (error) => {
if (error.status === 0 && error.error instanceof ProgressEvent && !req.url.includes('UserAuthentication/Login')) {
// A client-side or network error occurred. Handle it accordingly.
this.backgroundservice.offline();
}
}
}),
// Log when response observable either completes or errors
finalize(() => {
// const elapsed = Date.now() - started;
// const msg = `${req.method} "${req.urlWithParams}"
// ${ok} in ${elapsed} ms.`;
})
);
}
}