Showing
7 changed files
with
24 additions
and
18 deletions
... | @@ -26,10 +26,13 @@ export class MessageHandlerRegistry { | ... | @@ -26,10 +26,13 @@ export class MessageHandlerRegistry { |
26 | ); | 26 | ); |
27 | } | 27 | } |
28 | 28 | ||
29 | - private static registerHandler<T extends Message>( | 29 | + private static registerHandler< |
30 | + T extends Message, | ||
31 | + S extends Message | undefined | ||
32 | + >( | ||
30 | connection: Connection, | 33 | connection: Connection, |
31 | typeName: string, | 34 | typeName: string, |
32 | - handler: (connection: Connection, message: T) => MessageResponse | 35 | + handler: (connection: Connection, message: T) => MessageResponse<S> |
33 | ) { | 36 | ) { |
34 | connection.socket.on(typeName, (message: T, callback: Function) => { | 37 | connection.socket.on(typeName, (message: T, callback: Function) => { |
35 | const response = handler(connection, message); | 38 | const response = handler(connection, message); |
... | @@ -37,10 +40,13 @@ export class MessageHandlerRegistry { | ... | @@ -37,10 +40,13 @@ export class MessageHandlerRegistry { |
37 | }); | 40 | }); |
38 | } | 41 | } |
39 | 42 | ||
40 | - private static registerHandlerAuthed<T extends Message>( | 43 | + private static registerHandlerAuthed< |
44 | + T extends Message, | ||
45 | + S extends Message | undefined | ||
46 | + >( | ||
41 | connection: Connection, | 47 | connection: Connection, |
42 | typeName: string, | 48 | typeName: string, |
43 | - handler: (user: User, message: T) => MessageResponse | 49 | + handler: (user: User, message: T) => MessageResponse<S> |
44 | ) { | 50 | ) { |
45 | connection.socket.on(typeName, (message: T, callback: Function) => { | 51 | connection.socket.on(typeName, (message: T, callback: Function) => { |
46 | if (connection.user !== undefined) { | 52 | if (connection.user !== undefined) { | ... | ... |
... | @@ -6,7 +6,7 @@ import { LoginMessage, MessageResponse } from "../types"; | ... | @@ -6,7 +6,7 @@ import { LoginMessage, MessageResponse } from "../types"; |
6 | export function loginHandler( | 6 | export function loginHandler( |
7 | connection: Connection, | 7 | connection: Connection, |
8 | message: LoginMessage | 8 | message: LoginMessage |
9 | -): MessageResponse { | 9 | +): MessageResponse<undefined> { |
10 | connection.user = new User(message.username, connection); | 10 | connection.user = new User(message.username, connection); |
11 | console.log(`User ${message.username} has logged in!`); | 11 | console.log(`User ${message.username} has logged in!`); |
12 | 12 | ... | ... |
... | @@ -6,7 +6,7 @@ import { MessageResponse, RoomChatMessage, RoomJoinMessage } from "../types"; | ... | @@ -6,7 +6,7 @@ import { MessageResponse, RoomChatMessage, RoomJoinMessage } from "../types"; |
6 | export function roomChatHandler( | 6 | export function roomChatHandler( |
7 | user: User, | 7 | user: User, |
8 | message: RoomChatMessage | 8 | message: RoomChatMessage |
9 | -): MessageResponse { | 9 | +): MessageResponse<undefined> { |
10 | user.room?.sendChat(user, message.message); | 10 | user.room?.sendChat(user, message.message); |
11 | return { ok: true }; | 11 | return { ok: true }; |
12 | } | 12 | } | ... | ... |
1 | import { Connection } from "../../connection/Connection"; | 1 | import { Connection } from "../../connection/Connection"; |
2 | import { RoomManager } from "../../room/RoomManager"; | 2 | import { RoomManager } from "../../room/RoomManager"; |
3 | import { User } from "../../user/User"; | 3 | import { User } from "../../user/User"; |
4 | -import { MessageResponse, RoomJoinMessage } from "../types"; | 4 | +import { MessageResponse, RoomInfoMessage, RoomJoinMessage } from "../types"; |
5 | 5 | ||
6 | export function roomJoinHandler( | 6 | export function roomJoinHandler( |
7 | user: User, | 7 | user: User, |
8 | message: RoomJoinMessage | 8 | message: RoomJoinMessage |
9 | -): MessageResponse { | 9 | +): MessageResponse<RoomInfoMessage> { |
10 | const room = RoomManager.instance().get(message.uuid); | 10 | const room = RoomManager.instance().get(message.uuid); |
11 | if (room !== undefined) { | 11 | if (room !== undefined) { |
12 | - room.connect(user); | 12 | + const roomInfoMessage = room.connect(user); |
13 | - return { ok: true }; | 13 | + return { ok: roomInfoMessage !== undefined, result: roomInfoMessage }; |
14 | } | 14 | } |
15 | return { ok: false }; | 15 | return { ok: false }; |
16 | } | 16 | } | ... | ... |
... | @@ -6,7 +6,7 @@ import { MessageResponse, RoomLeaveMessage } from "../types"; | ... | @@ -6,7 +6,7 @@ import { MessageResponse, RoomLeaveMessage } from "../types"; |
6 | export function roomLeaveHandler( | 6 | export function roomLeaveHandler( |
7 | user: User, | 7 | user: User, |
8 | message: RoomLeaveMessage | 8 | message: RoomLeaveMessage |
9 | -): MessageResponse { | 9 | +): MessageResponse<undefined> { |
10 | user.room?.disconnect(user); | 10 | user.room?.disconnect(user); |
11 | return { ok: true }; | 11 | return { ok: true }; |
12 | } | 12 | } | ... | ... |
... | @@ -10,9 +10,10 @@ export interface Message { | ... | @@ -10,9 +10,10 @@ export interface Message { |
10 | * @param ok 요청의 성공 여부입니다. | 10 | * @param ok 요청의 성공 여부입니다. |
11 | * @param reason 요청 실패 사유입니다. 필요한 경우에만 포함됩니다. | 11 | * @param reason 요청 실패 사유입니다. 필요한 경우에만 포함됩니다. |
12 | */ | 12 | */ |
13 | -export interface MessageResponse { | 13 | +export interface MessageResponse<T> { |
14 | ok: boolean; | 14 | ok: boolean; |
15 | reason?: string; | 15 | reason?: string; |
16 | + result?: T; | ||
16 | } | 17 | } |
17 | 18 | ||
18 | /** | 19 | /** |
... | @@ -87,7 +88,6 @@ export class RoomChatMessage implements Message { | ... | @@ -87,7 +88,6 @@ export class RoomChatMessage implements Message { |
87 | } | 88 | } |
88 | 89 | ||
89 | export class MessageType { | 90 | export class MessageType { |
90 | - static readonly RESPONSE = "response"; | ||
91 | static readonly LOGIN = "login"; | 91 | static readonly LOGIN = "login"; |
92 | static readonly ROOM_LIST = "room_list"; | 92 | static readonly ROOM_LIST = "room_list"; |
93 | static readonly ROOM_JOIN = "room_join"; | 93 | static readonly ROOM_JOIN = "room_join"; | ... | ... |
... | @@ -26,23 +26,23 @@ export class Room { | ... | @@ -26,23 +26,23 @@ export class Room { |
26 | this.maxUsers = maxUsers; | 26 | this.maxUsers = maxUsers; |
27 | } | 27 | } |
28 | 28 | ||
29 | - public connect(user: User): void { | 29 | + public connect(user: User): RoomInfoMessage | undefined { |
30 | if (this.users.includes(user) || this.users.length >= this.maxUsers) { | 30 | if (this.users.includes(user) || this.users.length >= this.maxUsers) { |
31 | - return; | 31 | + return undefined; |
32 | } | 32 | } |
33 | 33 | ||
34 | + this.broadcast(new RoomUserUpdateMessage("added", user.getData())); | ||
35 | + | ||
34 | this.users.push(user); | 36 | this.users.push(user); |
35 | user.room = this; // TODO: 더 나은 관리 | 37 | user.room = this; // TODO: 더 나은 관리 |
36 | 38 | ||
37 | - this.broadcast(new RoomUserUpdateMessage("added", user.getData())); | ||
38 | - | ||
39 | var users: UserData[] = []; | 39 | var users: UserData[] = []; |
40 | this.users.forEach((u) => { | 40 | this.users.forEach((u) => { |
41 | if (user !== u) { | 41 | if (user !== u) { |
42 | users.push(u.getData()); | 42 | users.push(u.getData()); |
43 | } | 43 | } |
44 | }); | 44 | }); |
45 | - user.connection.send(new RoomInfoMessage(users)); | 45 | + return new RoomInfoMessage(users); |
46 | } | 46 | } |
47 | 47 | ||
48 | public disconnect(user: User): void { | 48 | public disconnect(user: User): void { | ... | ... |
-
Please register or login to post a comment