94 lines
3.2 KiB
TypeScript
94 lines
3.2 KiB
TypeScript
import { defineStore } from "pinia";
|
|
import { io, Socket } from "socket.io-client";
|
|
import { useNotificationStore } from "../../notification";
|
|
import { refreshToken, url } from "../../../serverCom";
|
|
|
|
export const useConnectionStore = defineStore("connection", {
|
|
state: () => {
|
|
return {
|
|
connection: undefined as undefined | Socket,
|
|
connected: false as boolean,
|
|
performingManualReconnect: false as boolean,
|
|
};
|
|
},
|
|
getters: {
|
|
connectionStatus: (state) => state.connected,
|
|
socketId: (state) => state.connection?.id,
|
|
},
|
|
actions: {
|
|
connectClient(): void {
|
|
if (this.connectionStatus) return;
|
|
const notificationStore = useNotificationStore();
|
|
this.connection?.disconnect();
|
|
this.connection = io(url, {
|
|
reconnection: false,
|
|
reconnectionDelayMax: 1000,
|
|
reconnectionAttempts: 1,
|
|
auth: (cb) => {
|
|
cb({ token: localStorage.getItem("accessToken") });
|
|
},
|
|
});
|
|
|
|
this.connection.on("connect", () => {
|
|
this.connected = true;
|
|
notificationStore.push("Socket-Erfolg", `Verbindung aufgebaut`, "success");
|
|
});
|
|
this.connection.on("connect_error", (err) => {
|
|
this.connected = false;
|
|
this.handleError(err, true);
|
|
});
|
|
this.connection.on("disconnecting", () => {
|
|
this.connected = false;
|
|
this.connection?.disconnect();
|
|
});
|
|
this.connection.on("disconnect", () => {
|
|
this.connected = false;
|
|
this.$reset();
|
|
if (!this.performingManualReconnect) {
|
|
this.connectClient();
|
|
}
|
|
});
|
|
this.connection.on("warning", (msg: string) => {
|
|
notificationStore.push("Socket-Warnung", msg, "warning");
|
|
});
|
|
this.connection.on("error", (msg: string) => {
|
|
this.handleError({
|
|
name: "Error",
|
|
message: msg,
|
|
});
|
|
});
|
|
},
|
|
disconnectClient(): void {
|
|
this.connection?.disconnect();
|
|
},
|
|
handleError(err: Error, onConnect = false) {
|
|
const notificationStore = useNotificationStore();
|
|
|
|
if (err.message == "xhr poll error") {
|
|
notificationStore.push("Socket-Netzwerk-Fehler", "Reconnect Versuch in 10s", "error");
|
|
this.performingManualReconnect = true;
|
|
this.disconnectClient();
|
|
setTimeout(() => {
|
|
this.connectClient();
|
|
this.performingManualReconnect = false;
|
|
}, 10000);
|
|
} else if (err.message == "Token expired") {
|
|
notificationStore.push("Session", "Session wird verlängert", "info");
|
|
refreshToken()
|
|
.then(() => {
|
|
notificationStore.push("Session", "Session erfolgreich verlängert", "success");
|
|
this.performingManualReconnect = true;
|
|
this.connection?.disconnect().connect();
|
|
this.performingManualReconnect = false;
|
|
})
|
|
.catch(() => {
|
|
notificationStore.push("Session-Fehler", "Anmeldung wurde nicht verlängert", "error");
|
|
});
|
|
} else if (onConnect) {
|
|
notificationStore.push("Socket-Fehler", `Verbindung fehlgeschlagen`, "error");
|
|
} else {
|
|
notificationStore.push("Socket-Fehler", err.message, "error");
|
|
}
|
|
},
|
|
},
|
|
});
|