Fixed socket service #70

This commit is contained in:
Sven Heidemann 2022-10-23 17:49:31 +02:00
parent ea026b351e
commit 24a3a48478

View File

@ -7,7 +7,7 @@ import io from "socket.io-client";
import { MessageService } from "primeng/api"; import { MessageService } from "primeng/api";
@Injectable({ @Injectable({
providedIn: 'root' providedIn: "root"
}) })
export class SocketService { export class SocketService {
private socket: any; private socket: any;
@ -17,15 +17,14 @@ export class SocketService {
private settingsService: SettingsService, private settingsService: SettingsService,
private toastService: ToastService, private toastService: ToastService,
private spinnerService: SpinnerService, private spinnerService: SpinnerService,
private messageService: MessageService, private messageService: MessageService
) { ) {
} }
startSocket() { startSocket() {
this.socket = io(`${this.settingsService.getApiURL()}`, {path: '/api/socket.io'}) this.socket = io(`${this.settingsService.getApiURL()}`, { path: "/api/socket.io" });
this.socket.on('connect', () => { this.socket.on("connect", () => {
if (this.disconnected) { if (this.disconnected) {
if (this.spinnerService.showSpinnerState) {
this.spinnerService.hideSpinner(); this.spinnerService.hideSpinner();
const options: ToastOptions = { const options: ToastOptions = {
closable: false closable: false
@ -33,20 +32,19 @@ export class SocketService {
this.messageService.clear(); this.messageService.clear();
this.toastService.info("Server verbunden", "Die Verbindung zum Server konnte hergestellt werden.", options); this.toastService.info("Server verbunden", "Die Verbindung zum Server konnte hergestellt werden.", options);
} }
}
this.disconnected = false; this.disconnected = false;
console.log('Connected!') console.info("Connected!");
}); });
this.socket.on('connect_error', (err: Error) => { this.socket.on("connect_error", (err: Error) => {
if (this.disconnected) { if (this.disconnected) {
this.spinnerService.showSpinner(); this.spinnerService.showSpinner();
return; return;
} }
console.warn("Connect error!");
this.disconnected = true; this.disconnected = true;
const options: ToastOptions = { const options: ToastOptions = {
sticky: true, sticky: true,
closable: false closable: false
@ -56,8 +54,13 @@ export class SocketService {
console.error(err.toString()); console.error(err.toString());
}); });
this.socket.on('disconnect', () => { this.socket.on("disconnect", () => {
console.log('Disconnected!'); if (this.disconnected) {
return;
}
console.warn("Disconnected!");
this.disconnected = true;
const options: ToastOptions = { const options: ToastOptions = {
sticky: true, sticky: true,
closable: false closable: false