(httpServer: http.Server)
| 8 | public static readonly socketsMap = new Map<string, Socket>(); |
| 9 | |
| 10 | public static setUpSocketIO(httpServer: http.Server) { |
| 11 | const io = new Server(httpServer, { |
| 12 | path: "/socket.io", |
| 13 | cors: { |
| 14 | // temporary |
| 15 | origin: "*", |
| 16 | methods: ["GET", "POST"], |
| 17 | credentials: true |
| 18 | }, |
| 19 | maxHttpBufferSize: 1e7 |
| 20 | }); |
| 21 | |
| 22 | io.on("connection", (socket) => { |
| 23 | // When the user is connected to the Websocket, join the global and bind related events |
| 24 | logger.info(`Websocket ${socket.id}(${socket.handshake.address}) connection`); |
| 25 | this.socketsMap.set(socket.id, socket); |
| 26 | // bind the business event |
| 27 | SocketService.bindEvents(socket); |
| 28 | // When the user Websocket is disconnected, delete from the Socket list and release some resources |
| 29 | socket.on("disconnect", () => { |
| 30 | this.socketsMap.delete(socket.id); |
| 31 | for (const name of socket.eventNames()) socket.removeAllListeners(name); |
| 32 | logger.info(`Websocket ${socket.id}(${socket.handshake.address}) disconnected`); |
| 33 | }); |
| 34 | }); |
| 35 | |
| 36 | this.server = io; |
| 37 | return this.server; |
| 38 | } |
| 39 | |
| 40 | // used to bind Socket events |
| 41 | private static bindEvents(socket: Socket) { |
nothing calls this directly
no test coverage detected