From 3e47d3ebf6f6e3a80c92cc0f8334e859e056dacd Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Tue, 15 Jul 2025 15:16:18 +0200 Subject: [PATCH] admin side scan connection --- src/enums/socketEnum.ts | 4 ++++ src/socketManager.ts | 30 +++++++++++++++++------------- src/stores/admin/scanner.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 src/enums/socketEnum.ts diff --git a/src/enums/socketEnum.ts b/src/enums/socketEnum.ts new file mode 100644 index 0000000..4aed946 --- /dev/null +++ b/src/enums/socketEnum.ts @@ -0,0 +1,4 @@ +export enum SocketConnectionTypes { + scanner = "/scanner", + pscanner = "/public_scanner", +} diff --git a/src/socketManager.ts b/src/socketManager.ts index 85c0c44..772efc6 100644 --- a/src/socketManager.ts +++ b/src/socketManager.ts @@ -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(); + private static readonly connections = new Map(); + + 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") { diff --git a/src/stores/admin/scanner.ts b/src/stores/admin/scanner.ts index 89d5008..1726a89 100644 --- a/src/stores/admin/scanner.ts +++ b/src/stores/admin/scanner.ts @@ -1,5 +1,8 @@ import { defineStore } from "pinia"; import { v4 as uuid } from "uuid"; +import { SocketManager } from "../../socketManager"; +import { SocketConnectionTypes } from "../../enums/socketEnum"; +import { useNotificationStore } from "../notification"; export const useScannerStore = defineStore("scanner", { state: () => { @@ -13,13 +16,40 @@ export const useScannerStore = defineStore("scanner", { actions: { startSession() { if (this.inUse) return; + + const notificationStore = useNotificationStore(); + this.roomId = uuid(); this.inUse = true; + let connection = SocketManager.establishConnection(SocketConnectionTypes.scanner); + connection.on("connect", () => { + SocketManager.getConnection(SocketConnectionTypes.scanner)?.emit("session:create", this.roomId); + }); + connection.on("status-session:create", () => { + notificationStore.push("Socket-Erfolg", `Scan-Session gestartet`, "success"); + }); + connection.on("status-session:close", () => { + notificationStore.push("Socket", `Scan-Session beendet`, "info"); + SocketManager.getConnection(SocketConnectionTypes.scanner)?.disconnect(); + }); + connection.on("package-scanner_join", (socketId: string) => { + this.connectedDevices++; + notificationStore.push("Scan-Verbindung", `Neuer Scanner verbunden`, "info"); + }); + connection.on("package-scanner_leave", (socketId: string) => { + this.connectedDevices--; + notificationStore.push("Scan-Verbindung", `Scanner getrennt`, "info"); + }); + connection.on("package-scan_receive", (result: string) => { + this.results.push(result); + notificationStore.push("Scan", `Neuen Scan erhalten`, "info"); + }); }, endSession() { this.inUse = false; this.roomId = undefined; this.results = []; + SocketManager.getConnection(SocketConnectionTypes.scanner)?.emit("session:close"); }, removeElementFromResults(el: string) { this.results = this.results.filter((result) => result !== el);