강동현

기초적인 방 관리 로직 구현

import { Connection } from "../connection/Connection";
import { loginHandler } from "./handler/LoginHandler";
import { loginHandler } from "./handler/loginHandler";
import { MessageType } from "./types";
export class MessageHandlerRegistry {
......
......@@ -3,11 +3,13 @@
"@types/express": "^4.17.11",
"@types/node": "^15.3.1",
"@types/socket.io": "^3.0.2",
"@types/uuid": "^8.3.0",
"express": "^4.17.1",
"nodemon": "^2.0.7",
"socket.io": "^4.1.2",
"ts-node": "^9.1.1",
"typescript": "^4.2.4"
"typescript": "^4.2.4",
"uuid": "^8.3.2"
},
"scripts": {
"start": "nodemon index.ts"
......
import { Connection } from "../connection/Connection";
import { v4 as uuidv4 } from "uuid";
export class Room {
public readonly uuid: string;
public name: string;
public readonly maxConnections: number;
private connections: Connection[] = [];
private closed: boolean = false;
constructor(name: string, maxConnections: number = 8) {
this.uuid = uuidv4();
this.name = name;
this.maxConnections = maxConnections;
}
public connect(connection: Connection): void {
if (
this.connections.includes(connection) ||
this.connections.length >= this.maxConnections
) {
return;
}
this.connections.push(connection);
}
public disconnect(connection: Connection): void {
const index = this.connections.indexOf(connection);
if (index > -1) {
this.connections.splice(index, 1);
}
}
public close(): void {
if (!this.closed) {
// TODO
this.closed = true;
}
}
}
import { Room } from "./Room";
export class RoomManager {
private rooms: Map<string, Room>;
constructor() {
this.rooms = new Map<string, Room>();
}
public create(name: string, maxConnections: number): Room {
const room = new Room(name, maxConnections);
this.rooms.set(name, room);
return room;
}
public get(uuid: string): Room | undefined {
return this.rooms.get(uuid);
}
public delete(uuid: string): void {
const room = this.get(uuid);
if (room !== undefined) {
room.close();
this.rooms.delete(uuid);
}
}
}
......@@ -103,6 +103,11 @@
dependencies:
socket.io "*"
"@types/uuid@^8.3.0":
version "8.3.0"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.0.tgz#215c231dff736d5ba92410e6d602050cce7e273f"
integrity sha512-eQ9qFW/fhfGJF8WKHGEHZEyVWfZxrT+6CLIJGBcZPfxUh/+BnEj+UCGYMlr9qZuX/2AltsvwrGqp0LhEW8D0zQ==
abbrev@1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"
......@@ -1349,6 +1354,11 @@ utils-merge@1.0.1:
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=
uuid@^8.3.2:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==
vary@^1, vary@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
......