ff-admin/src/socketManager.ts

86 lines
3 KiB
TypeScript
Raw Normal View History

2025-07-15 11:52:44 +02:00
import { Manager, Socket } from "socket.io-client";
import { refreshToken, url } from "./serverCom";
import { useNotificationStore } from "./stores/notification";
2025-07-15 15:16:18 +02:00
import { SocketConnectionTypes } from "./enums/socketEnum";
2025-07-15 11:52:44 +02:00
export abstract class SocketManager {
2025-07-15 15:16:18 +02:00
private static readonly manager = new Manager(url, {
2025-07-15 11:52:44 +02:00
reconnection: true,
reconnectionDelayMax: 10000,
});
2025-07-15 15:16:18 +02:00
private static readonly connections = new Map<SocketConnectionTypes, Socket>();
public static establishConnection(
connection: SocketConnectionTypes,
restoreAfterDisconnect: boolean = false
): Socket {
const existingSocket = this.connections.get(connection);
if (existingSocket !== undefined && existingSocket.connected) return existingSocket!;
existingSocket?.removeAllListeners();
2025-07-15 11:52:44 +02:00
const notificationStore = useNotificationStore();
let socket = this.manager.socket(connection, {
auth: (cb) => {
cb({ token: localStorage.getItem("accessToken") });
},
});
socket.on("connect", () => {
notificationStore.push("Socket-Erfolg", `Verbindung aufgebaut`, "success");
});
socket.on("connect_error", (err) => {
this.socketHandleError(connection, err, true);
});
socket.on("disconnect", () => {
2025-07-15 15:16:18 +02:00
if (restoreAfterDisconnect) this.establishConnection(connection);
else notificationStore.push("Socket", `Verbindung getrennt`, "info");
2025-07-15 11:52:44 +02:00
});
socket.on("warning", (msg: string) => {
notificationStore.push("Socket-Warnung", msg, "warning");
});
socket.on("error", (msg: string) => {
this.socketHandleError(connection, {
name: "Error",
message: msg,
});
});
this.connections.set(connection, socket);
return socket;
}
2025-07-15 15:16:18 +02:00
public static getConnection(connection: SocketConnectionTypes) {
2025-07-15 11:52:44 +02:00
return this.connections.get(connection);
}
2025-07-15 15:16:18 +02:00
public static closeConnection(connection: SocketConnectionTypes) {
2025-07-15 11:52:44 +02:00
let socket = this.connections.get(connection);
if (socket) {
socket.disconnect();
this.connections.delete(connection);
}
}
2025-07-15 15:16:18 +02:00
private static socketHandleError(connection: SocketConnectionTypes, err: Error, onConnect = false) {
2025-07-15 11:52:44 +02:00
const notificationStore = useNotificationStore();
if (err.message == "xhr poll error") {
notificationStore.push("Socket-Netzwerk-Fehler", "Reconnect Versuch in 10s", "error");
} 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.closeConnection(connection);
})
.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");
}
}
}