34 lines
945 B
TypeScript
34 lines
945 B
TypeScript
|
import helmet from "helmet";
|
||
|
import { Server as httpServerType } from "http";
|
||
|
import { Server } from "socket.io";
|
||
|
import authenticateSocket from "../middleware/authenticateSocket";
|
||
|
import checkSocketExists from "../middleware/checkSocketExists";
|
||
|
import base from "./base";
|
||
|
|
||
|
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,
|
||
|
},
|
||
|
});
|
||
|
|
||
|
this.io.engine.use(helmet());
|
||
|
|
||
|
this.io
|
||
|
.of("/scanner")
|
||
|
.use(authenticateSocket)
|
||
|
.on("connection", (socket) => {
|
||
|
console.log("socket connection: ", socket.id);
|
||
|
socket.use((packet, next) => authenticateSocket(socket, next));
|
||
|
socket.use((packet, next) => checkSocketExists(socket, packet, next));
|
||
|
|
||
|
base(this.io, socket);
|
||
|
});
|
||
|
}
|
||
|
}
|