add basic socketio to server
This commit is contained in:
parent
45ec6b856a
commit
b29cdae088
8 changed files with 252 additions and 1 deletions
33
src/websocket/index.ts
Normal file
33
src/websocket/index.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
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);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue