admin side scan connection
This commit is contained in:
parent
ed947e5777
commit
3e47d3ebf6
3 changed files with 51 additions and 13 deletions
|
@ -1,21 +1,24 @@
|
|||
import { Manager, Socket } from "socket.io-client";
|
||||
import { refreshToken, url } from "./serverCom";
|
||||
import { useNotificationStore } from "./stores/notification";
|
||||
|
||||
export enum SocketConnectionTypes {
|
||||
scanner = "/scanner",
|
||||
pscanner = "/public_scanner",
|
||||
}
|
||||
import { SocketConnectionTypes } from "./enums/socketEnum";
|
||||
|
||||
export abstract class SocketManager {
|
||||
private readonly manager = new Manager(url, {
|
||||
private static readonly manager = new Manager(url, {
|
||||
reconnection: true,
|
||||
reconnectionDelayMax: 10000,
|
||||
});
|
||||
private readonly connections = new Map<SocketConnectionTypes, Socket>();
|
||||
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();
|
||||
|
||||
public establishConnection(connection: SocketConnectionTypes) {
|
||||
if (this.connections.has(connection)) return this.connections.get(connection);
|
||||
const notificationStore = useNotificationStore();
|
||||
let socket = this.manager.socket(connection, {
|
||||
auth: (cb) => {
|
||||
|
@ -29,7 +32,8 @@ export abstract class SocketManager {
|
|||
this.socketHandleError(connection, err, true);
|
||||
});
|
||||
socket.on("disconnect", () => {
|
||||
this.establishConnection(connection);
|
||||
if (restoreAfterDisconnect) this.establishConnection(connection);
|
||||
else notificationStore.push("Socket", `Verbindung getrennt`, "info");
|
||||
});
|
||||
socket.on("warning", (msg: string) => {
|
||||
notificationStore.push("Socket-Warnung", msg, "warning");
|
||||
|
@ -45,11 +49,11 @@ export abstract class SocketManager {
|
|||
return socket;
|
||||
}
|
||||
|
||||
public getConnection(connection: SocketConnectionTypes) {
|
||||
public static getConnection(connection: SocketConnectionTypes) {
|
||||
return this.connections.get(connection);
|
||||
}
|
||||
|
||||
public closeConnection(connection: SocketConnectionTypes) {
|
||||
public static closeConnection(connection: SocketConnectionTypes) {
|
||||
let socket = this.connections.get(connection);
|
||||
if (socket) {
|
||||
socket.disconnect();
|
||||
|
@ -57,7 +61,7 @@ export abstract class SocketManager {
|
|||
}
|
||||
}
|
||||
|
||||
private socketHandleError(connection: SocketConnectionTypes, err: Error, onConnect = false) {
|
||||
private static socketHandleError(connection: SocketConnectionTypes, err: Error, onConnect = false) {
|
||||
const notificationStore = useNotificationStore();
|
||||
|
||||
if (err.message == "xhr poll error") {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue