강동현

Server 클래스 작성

1 +import express from "express";
2 +import socketIo, { Server as IoServer } from "socket.io";
3 +import { createServer } from "http";
4 +import { SocketHandler } from "./SocketHandler";
5 +import { RoomManager } from "./room/RoomManager";
6 +
7 +export class Server {
8 + public readonly port: number;
9 + private readonly io: IoServer;
10 +
11 + constructor(port: number) {
12 + this.port = port;
13 +
14 + const app = express();
15 + const server = createServer(app);
16 + this.io = new socketIo.Server(server);
17 +
18 + const handler = new SocketHandler();
19 + const roomManager = new RoomManager();
20 +
21 + roomManager.create("테스트 방 #1", 8);
22 + roomManager.create("테스트 방 #2", 4);
23 + roomManager.create("테스트 방 #3", 2);
24 +
25 + this.io.on("connection", (socket) => {
26 + handler.connected(socket);
27 + });
28 +
29 + server.listen(port, () => console.log(`Listening on ${port}`));
30 + }
31 +
32 + public close(): void {
33 + this.io.close();
34 + }
35 +}
1 -import express from "express"; 1 +import { Server } from "./Server";
2 -import socketIo from "socket.io";
3 -import { createServer } from "http";
4 -import { SocketHandler } from "./SocketHandler";
5 -import { RoomManager } from "./room/RoomManager";
6 2
7 -const PORT = 3000; 3 +new Server(3000);
8 -
9 -const app = express();
10 -const server = createServer(app);
11 -const io = new socketIo.Server(server);
12 -
13 -const handler = new SocketHandler();
14 -const roomManager = new RoomManager();
15 -
16 -roomManager.create("테스트 방 #1", 8);
17 -roomManager.create("테스트 방 #2", 4);
18 -roomManager.create("테스트 방 #3", 2);
19 -
20 -io.on("connection", (socket) => {
21 - handler.connected(socket);
22 -});
23 -
24 -server.listen(PORT, () => console.log(`Listening on ${PORT}`));
......