Added api connection check #70
This commit is contained in:
16
kdb-web/src/app/services/socket/socket.service.spec.ts
Normal file
16
kdb-web/src/app/services/socket/socket.service.spec.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { SocketService } from './socket.service';
|
||||
|
||||
describe('SocketService', () => {
|
||||
let service: SocketService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(SocketService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
71
kdb-web/src/app/services/socket/socket.service.ts
Normal file
71
kdb-web/src/app/services/socket/socket.service.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { ToastOptions } from 'src/app/models/utils/toast-options';
|
||||
import { SettingsService } from '../settings/settings.service';
|
||||
import { SpinnerService } from '../spinner/spinner.service';
|
||||
import { ToastService } from '../toast/toast.service';
|
||||
import io from "socket.io-client";
|
||||
import { MessageService } from 'primeng/api';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class SocketService {
|
||||
private socket: any;
|
||||
disconnected = false;
|
||||
|
||||
constructor(
|
||||
private settingsService: SettingsService,
|
||||
private toastService: ToastService,
|
||||
private spinnerService: SpinnerService,
|
||||
private messageService: MessageService,
|
||||
) {
|
||||
this.socket = io(`${settingsService.getApiURL()}`)
|
||||
}
|
||||
|
||||
startSocket() {
|
||||
this.socket.on('connect', () => {
|
||||
if (this.disconnected) {
|
||||
if (this.spinnerService.showSpinnerState) {
|
||||
this.spinnerService.hideSpinner();
|
||||
const options: ToastOptions = {
|
||||
closable: false
|
||||
};
|
||||
this.messageService.clear();
|
||||
this.toastService.info("Server verbunden", "Die Verbindung zum Server konnte hergestellt werden.", options);
|
||||
}
|
||||
}
|
||||
|
||||
this.disconnected = false;
|
||||
console.log('Connected!')
|
||||
});
|
||||
|
||||
this.socket.on('connect_error', (err: Error) => {
|
||||
if (this.disconnected) {
|
||||
this.spinnerService.showSpinner();
|
||||
return;
|
||||
}
|
||||
|
||||
this.disconnected = true;
|
||||
|
||||
const options: ToastOptions = {
|
||||
sticky: true,
|
||||
closable: false
|
||||
};
|
||||
this.messageService.clear();
|
||||
this.toastService.error("Server nicht erreichbar", "Die Verbindung zum Server konnte nicht hergestellt werden!\nLaden Sie die Seite neu.", options);
|
||||
console.error(err.toString());
|
||||
});
|
||||
|
||||
this.socket.on('disconnect', () => {
|
||||
console.log('Disconnected!');
|
||||
const options: ToastOptions = {
|
||||
sticky: true,
|
||||
closable: false
|
||||
};
|
||||
this.spinnerService.showSpinner();
|
||||
this.messageService.clear();
|
||||
this.toastService.error("Verbindung unterbrochen", "Die Verbindung zum Server konnte nicht hergestellt werden!\nLaden Sie die Seite neu.", options);
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user