Showing
4 changed files
with
62 additions
and
1 deletions
1 | import { RoomData } from "../room/types"; | 1 | import { RoomData } from "../room/types"; |
2 | +import { UserData } from "../user/types"; | ||
2 | 3 | ||
3 | export interface Message { | 4 | export interface Message { |
4 | readonly type: string; | 5 | readonly type: string; |
... | @@ -24,9 +25,24 @@ export class RoomLeaveMessage implements Message { | ... | @@ -24,9 +25,24 @@ export class RoomLeaveMessage implements Message { |
24 | constructor() {} | 25 | constructor() {} |
25 | } | 26 | } |
26 | 27 | ||
28 | +export class RoomInfoMessage implements Message { | ||
29 | + readonly type = MessageType.ROOM_INFO; | ||
30 | + constructor(public userdata: UserData[]) {} | ||
31 | +} | ||
32 | + | ||
33 | +export class RoomUserUpdateMessage implements Message { | ||
34 | + readonly type = MessageType.ROOM_USER_UPDATE; | ||
35 | + constructor( | ||
36 | + public state: "added" | "updated" | "removed", | ||
37 | + public userdata: UserData | ||
38 | + ) {} | ||
39 | +} | ||
40 | + | ||
27 | export class MessageType { | 41 | export class MessageType { |
28 | static readonly LOGIN = "login"; | 42 | static readonly LOGIN = "login"; |
29 | static readonly ROOM_LIST = "room_list"; | 43 | static readonly ROOM_LIST = "room_list"; |
30 | static readonly ROOM_JOIN = "room_join"; | 44 | static readonly ROOM_JOIN = "room_join"; |
31 | static readonly ROOM_LEAVE = "room_leave"; | 45 | static readonly ROOM_LEAVE = "room_leave"; |
46 | + static readonly ROOM_INFO = "room_info"; | ||
47 | + static readonly ROOM_USER_UPDATE = "room_user_update"; | ||
32 | } | 48 | } | ... | ... |
1 | import { Connection } from "../connection/Connection"; | 1 | import { Connection } from "../connection/Connection"; |
2 | import { v4 as uuidv4 } from "uuid"; | 2 | import { v4 as uuidv4 } from "uuid"; |
3 | import { RoomData } from "./types"; | 3 | import { RoomData } from "./types"; |
4 | +import { | ||
5 | + Message, | ||
6 | + RoomInfoMessage, | ||
7 | + RoomUserUpdateMessage, | ||
8 | +} from "../message/types"; | ||
9 | +import { UserData } from "../user/types"; | ||
4 | 10 | ||
5 | export class Room { | 11 | export class Room { |
6 | public readonly uuid: string; | 12 | public readonly uuid: string; |
... | @@ -19,6 +25,12 @@ export class Room { | ... | @@ -19,6 +25,12 @@ export class Room { |
19 | } | 25 | } |
20 | 26 | ||
21 | public connect(connection: Connection): void { | 27 | public connect(connection: Connection): void { |
28 | + // TODO: 더 나은 인증 처리 | ||
29 | + const user = connection.user; | ||
30 | + if (user === undefined) { | ||
31 | + return; | ||
32 | + } | ||
33 | + | ||
22 | if ( | 34 | if ( |
23 | this.connections.includes(connection) || | 35 | this.connections.includes(connection) || |
24 | this.connections.length >= this.maxConnections | 36 | this.connections.length >= this.maxConnections |
... | @@ -28,13 +40,27 @@ export class Room { | ... | @@ -28,13 +40,27 @@ export class Room { |
28 | 40 | ||
29 | this.connections.push(connection); | 41 | this.connections.push(connection); |
30 | connection.room = this; // TODO: 더 나은 관리 | 42 | connection.room = this; // TODO: 더 나은 관리 |
43 | + | ||
44 | + this.broadcast(new RoomUserUpdateMessage("added", user.getData())); | ||
45 | + | ||
46 | + var users: UserData[] = []; | ||
47 | + this.connections.forEach((con) => { | ||
48 | + if (con.user !== undefined && connection !== con) { | ||
49 | + users.push(con.user.getData()); | ||
50 | + } | ||
51 | + }); | ||
52 | + connection.send(new RoomInfoMessage(users)); | ||
31 | } | 53 | } |
32 | 54 | ||
33 | public disconnect(connection: Connection): void { | 55 | public disconnect(connection: Connection): void { |
34 | const index = this.connections.indexOf(connection); | 56 | const index = this.connections.indexOf(connection); |
35 | - if (index > -1) { | 57 | + if (connection.user !== undefined && index > -1) { |
36 | this.connections.splice(index, 1); | 58 | this.connections.splice(index, 1); |
37 | connection.room = undefined; | 59 | connection.room = undefined; |
60 | + | ||
61 | + this.broadcast( | ||
62 | + new RoomUserUpdateMessage("removed", connection.user.getData()) | ||
63 | + ); | ||
38 | } | 64 | } |
39 | } | 65 | } |
40 | 66 | ||
... | @@ -47,6 +73,14 @@ export class Room { | ... | @@ -47,6 +73,14 @@ export class Room { |
47 | }; | 73 | }; |
48 | } | 74 | } |
49 | 75 | ||
76 | + public broadcast(message: Message, except?: Connection): void { | ||
77 | + this.connections.forEach((connection) => { | ||
78 | + if (connection !== except) { | ||
79 | + connection.send(message); | ||
80 | + } | ||
81 | + }); | ||
82 | + } | ||
83 | + | ||
50 | public close(): void { | 84 | public close(): void { |
51 | if (!this.closed) { | 85 | if (!this.closed) { |
52 | this.connections.forEach((connection) => this.disconnect(connection)); | 86 | this.connections.forEach((connection) => this.disconnect(connection)); | ... | ... |
1 | +import { UserData } from "./types"; | ||
2 | + | ||
1 | export class User { | 3 | export class User { |
2 | public readonly username: string; | 4 | public readonly username: string; |
3 | 5 | ||
4 | constructor(username: string) { | 6 | constructor(username: string) { |
5 | this.username = username; | 7 | this.username = username; |
6 | } | 8 | } |
9 | + | ||
10 | + public getData(): UserData { | ||
11 | + return { | ||
12 | + username: this.username, | ||
13 | + }; | ||
14 | + } | ||
7 | } | 15 | } | ... | ... |
server/user/types.ts
0 → 100644
-
Please register or login to post a comment