ff-admin-server/src/websocket/index.ts

54 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-07-15 11:52:58 +02:00
import helmet from "helmet";
import { Server as httpServerType } from "http";
import { Server } from "socket.io";
2025-07-15 15:16:11 +02:00
import { instrument } from "@socket.io/admin-ui";
2025-07-15 11:52:58 +02:00
import authenticateSocket from "../middleware/authenticateSocket";
import checkSocketExists from "../middleware/checkSocketExists";
2025-07-15 15:16:11 +02:00
import { SocketConnectionTypes } from "../enums/socketEnum";
2025-07-15 11:52:58 +02:00
import base from "./base";
2025-07-15 15:16:11 +02:00
import scanner from "./scanner";
import pScanner from "./pScanner";
2025-07-15 11:52:58 +02:00
export default abstract class SocketServer {
private static io: Server;
public static init(httpServer: httpServerType) {
this.io = new Server(httpServer, {
cors: {
origin: "*",
methods: ["GET", "POST"],
credentials: true,
},
});
2025-07-15 15:16:11 +02:00
if (process.env.NODE_ENV) {
instrument(this.io, {
auth: false,
mode: "development",
});
}
2025-07-15 11:52:58 +02:00
this.io.engine.use(helmet());
this.io
2025-07-15 15:16:11 +02:00
.of(SocketConnectionTypes.scanner)
2025-07-15 11:52:58 +02:00
.use(authenticateSocket)
.on("connection", (socket) => {
console.log("socket connection: ", socket.id);
socket.use((packet, next) => checkSocketExists(socket, packet, next));
base(this.io, socket);
2025-07-15 15:16:11 +02:00
scanner(this.io, socket);
2025-07-15 11:52:58 +02:00
});
2025-07-15 15:16:11 +02:00
this.io.of(SocketConnectionTypes.pscanner).on("connection", (socket) => {
console.log("socket connection: ", socket.id);
base(this.io, socket);
pScanner(this.io, socket);
});
2025-07-15 11:52:58 +02:00
}
}