Showing
5 changed files
with
85 additions
and
2 deletions
1 | import { Connection } from "../connection/Connection"; | 1 | import { Connection } from "../connection/Connection"; |
2 | -import { loginHandler } from "./handler/LoginHandler"; | 2 | +import { loginHandler } from "./handler/loginHandler"; |
3 | import { MessageType } from "./types"; | 3 | import { MessageType } from "./types"; |
4 | 4 | ||
5 | export class MessageHandlerRegistry { | 5 | export class MessageHandlerRegistry { | ... | ... |
... | @@ -3,11 +3,13 @@ | ... | @@ -3,11 +3,13 @@ |
3 | "@types/express": "^4.17.11", | 3 | "@types/express": "^4.17.11", |
4 | "@types/node": "^15.3.1", | 4 | "@types/node": "^15.3.1", |
5 | "@types/socket.io": "^3.0.2", | 5 | "@types/socket.io": "^3.0.2", |
6 | + "@types/uuid": "^8.3.0", | ||
6 | "express": "^4.17.1", | 7 | "express": "^4.17.1", |
7 | "nodemon": "^2.0.7", | 8 | "nodemon": "^2.0.7", |
8 | "socket.io": "^4.1.2", | 9 | "socket.io": "^4.1.2", |
9 | "ts-node": "^9.1.1", | 10 | "ts-node": "^9.1.1", |
10 | - "typescript": "^4.2.4" | 11 | + "typescript": "^4.2.4", |
12 | + "uuid": "^8.3.2" | ||
11 | }, | 13 | }, |
12 | "scripts": { | 14 | "scripts": { |
13 | "start": "nodemon index.ts" | 15 | "start": "nodemon index.ts" | ... | ... |
server/room/Room.ts
0 → 100644
1 | +import { Connection } from "../connection/Connection"; | ||
2 | +import { v4 as uuidv4 } from "uuid"; | ||
3 | + | ||
4 | +export class Room { | ||
5 | + public readonly uuid: string; | ||
6 | + | ||
7 | + public name: string; | ||
8 | + public readonly maxConnections: number; | ||
9 | + | ||
10 | + private connections: Connection[] = []; | ||
11 | + | ||
12 | + private closed: boolean = false; | ||
13 | + | ||
14 | + constructor(name: string, maxConnections: number = 8) { | ||
15 | + this.uuid = uuidv4(); | ||
16 | + this.name = name; | ||
17 | + this.maxConnections = maxConnections; | ||
18 | + } | ||
19 | + | ||
20 | + public connect(connection: Connection): void { | ||
21 | + if ( | ||
22 | + this.connections.includes(connection) || | ||
23 | + this.connections.length >= this.maxConnections | ||
24 | + ) { | ||
25 | + return; | ||
26 | + } | ||
27 | + | ||
28 | + this.connections.push(connection); | ||
29 | + } | ||
30 | + | ||
31 | + public disconnect(connection: Connection): void { | ||
32 | + const index = this.connections.indexOf(connection); | ||
33 | + if (index > -1) { | ||
34 | + this.connections.splice(index, 1); | ||
35 | + } | ||
36 | + } | ||
37 | + | ||
38 | + public close(): void { | ||
39 | + if (!this.closed) { | ||
40 | + // TODO | ||
41 | + this.closed = true; | ||
42 | + } | ||
43 | + } | ||
44 | +} |
server/room/RoomManager.ts
0 → 100644
1 | +import { Room } from "./Room"; | ||
2 | + | ||
3 | +export class RoomManager { | ||
4 | + private rooms: Map<string, Room>; | ||
5 | + | ||
6 | + constructor() { | ||
7 | + this.rooms = new Map<string, Room>(); | ||
8 | + } | ||
9 | + | ||
10 | + public create(name: string, maxConnections: number): Room { | ||
11 | + const room = new Room(name, maxConnections); | ||
12 | + this.rooms.set(name, room); | ||
13 | + return room; | ||
14 | + } | ||
15 | + | ||
16 | + public get(uuid: string): Room | undefined { | ||
17 | + return this.rooms.get(uuid); | ||
18 | + } | ||
19 | + | ||
20 | + public delete(uuid: string): void { | ||
21 | + const room = this.get(uuid); | ||
22 | + if (room !== undefined) { | ||
23 | + room.close(); | ||
24 | + this.rooms.delete(uuid); | ||
25 | + } | ||
26 | + } | ||
27 | +} |
... | @@ -103,6 +103,11 @@ | ... | @@ -103,6 +103,11 @@ |
103 | dependencies: | 103 | dependencies: |
104 | socket.io "*" | 104 | socket.io "*" |
105 | 105 | ||
106 | +"@types/uuid@^8.3.0": | ||
107 | + version "8.3.0" | ||
108 | + resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.0.tgz#215c231dff736d5ba92410e6d602050cce7e273f" | ||
109 | + integrity sha512-eQ9qFW/fhfGJF8WKHGEHZEyVWfZxrT+6CLIJGBcZPfxUh/+BnEj+UCGYMlr9qZuX/2AltsvwrGqp0LhEW8D0zQ== | ||
110 | + | ||
106 | abbrev@1: | 111 | abbrev@1: |
107 | version "1.1.1" | 112 | version "1.1.1" |
108 | resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8" | 113 | resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8" |
... | @@ -1349,6 +1354,11 @@ utils-merge@1.0.1: | ... | @@ -1349,6 +1354,11 @@ utils-merge@1.0.1: |
1349 | resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713" | 1354 | resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713" |
1350 | integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM= | 1355 | integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM= |
1351 | 1356 | ||
1357 | +uuid@^8.3.2: | ||
1358 | + version "8.3.2" | ||
1359 | + resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2" | ||
1360 | + integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg== | ||
1361 | + | ||
1352 | vary@^1, vary@~1.1.2: | 1362 | vary@^1, vary@~1.1.2: |
1353 | version "1.1.2" | 1363 | version "1.1.2" |
1354 | resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc" | 1364 | resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc" | ... | ... |
-
Please register or login to post a comment