admin side scan connection
This commit is contained in:
parent
ed947e5777
commit
3e47d3ebf6
3 changed files with 51 additions and 13 deletions
4
src/enums/socketEnum.ts
Normal file
4
src/enums/socketEnum.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
export enum SocketConnectionTypes {
|
||||
scanner = "/scanner",
|
||||
pscanner = "/public_scanner",
|
||||
}
|
|
@ -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") {
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue