Server.ts 913 Bytes
import express from "express";
import socketIo, { Server as IoServer } from "socket.io";
import { createServer } from "http";
import { SocketHandler } from "./SocketHandler";
import { RoomManager } from "./room/RoomManager";

export class Server {
  public readonly port: number;
  private readonly io: IoServer;

  constructor(port: number) {
    this.port = port;

    const app = express();
    const server = createServer(app);
    this.io = new socketIo.Server(server);

    const handler = new SocketHandler();
    const roomManager = new RoomManager();

    roomManager.create("테스트 방 #1", 8);
    roomManager.create("테스트 방 #2", 4);
    roomManager.create("테스트 방 #3", 2);

    this.io.on("connection", (socket) => {
      handler.connected(socket);
    });

    server.listen(port, () => console.log(`Listening on ${port}`));
  }

  public close(): void {
    this.io.close();
  }
}