ff-operation/src/stores/admin/operation/connection.ts

64 lines
2.1 KiB
TypeScript
Raw Normal View History

2025-02-21 11:55:49 +01:00
import { defineStore } from "pinia";
import { io, type Socket } from "socket.io-client";
import { useNotificationStore } from "../../notification";
import { url } from "../../../serverCom";
export const useConnectionStore = defineStore("connection", {
state: () => {
return {
connection: undefined as undefined | Socket,
};
},
getters: {
connectionStatus: (state) => !!state.connection,
},
actions: {
connectClient(): void {
if (this.connectionStatus) return;
const notificationStore = useNotificationStore();
this.connection?.disconnect();
this.connection = io(url, {
reconnection: true,
reconnectionDelayMax: 10000,
auth: {
token: localStorage.getItem("accessToken"),
},
});
this.connection.on("connect", () => {
notificationStore.push("Erfolg", `Verbindung aufgebaut`, "success");
});
this.connection.on("connect_error", () => {
notificationStore.push("Fehler", `Verbindung fehlgeschlagen`, "error");
});
this.connection.on("disconnecting", () => {
this.connection?.disconnect();
});
this.connection.on("disconnect", () => {
this.$reset();
});
this.connection.on("warning", (msg: string) => {
notificationStore.push("Warnung", msg, "warning");
});
this.connection.on("error", (msg: string) => {
notificationStore.push("Fehler", msg, "error");
});
this.connection.on("reconnect", (attemptNumber) => {
notificationStore.push("Reconnect", `Reconnect erfolgreich`, "success");
});
this.connection.on("reconnect_attempt", (attemptNumber) => {
notificationStore.push("Reconnect-Versuch", `Reconnect-Versuch Nr.${attemptNumber}`, "info");
});
this.connection.on("reconnect_error", (error) => {
notificationStore.push("Warnung", "Reconnect fehlgeschlagen", "warning");
});
this.connection.on("reconnect_failed", () => {
notificationStore.push("Fehler", "Reconnect-Versuche erfolglos beendet", "error");
});
},
disconnectClient(): void {
this.connection?.disconnect();
},
},
});