강동현

MessageResponse 구현

...@@ -4,7 +4,7 @@ import { loginHandler } from "./handler/loginHandler"; ...@@ -4,7 +4,7 @@ import { loginHandler } from "./handler/loginHandler";
4 import { roomChatHandler } from "./handler/roomChatHandler"; 4 import { roomChatHandler } from "./handler/roomChatHandler";
5 import { roomJoinHandler } from "./handler/roomJoinHandler"; 5 import { roomJoinHandler } from "./handler/roomJoinHandler";
6 import { roomLeaveHandler } from "./handler/roomLeaveHandler"; 6 import { roomLeaveHandler } from "./handler/roomLeaveHandler";
7 -import { Message, MessageType } from "./types"; 7 +import { Message, MessageResponse, MessageType } from "./types";
8 8
9 export class MessageHandlerRegistry { 9 export class MessageHandlerRegistry {
10 static registerHandlers(connection: Connection) { 10 static registerHandlers(connection: Connection) {
...@@ -29,21 +29,25 @@ export class MessageHandlerRegistry { ...@@ -29,21 +29,25 @@ export class MessageHandlerRegistry {
29 private static registerHandler<T extends Message>( 29 private static registerHandler<T extends Message>(
30 connection: Connection, 30 connection: Connection,
31 typeName: string, 31 typeName: string,
32 - handler: (connection: Connection, message: T) => void 32 + handler: (connection: Connection, message: T) => MessageResponse
33 ) { 33 ) {
34 - connection.socket.on(typeName, (message: T) => { 34 + connection.socket.on(typeName, (message: T, callback: Function) => {
35 - handler(connection, message); 35 + const response = handler(connection, message);
36 + callback(response);
36 }); 37 });
37 } 38 }
38 39
39 private static registerHandlerAuthed<T extends Message>( 40 private static registerHandlerAuthed<T extends Message>(
40 connection: Connection, 41 connection: Connection,
41 typeName: string, 42 typeName: string,
42 - handler: (user: User, message: T) => void 43 + handler: (user: User, message: T) => MessageResponse
43 ) { 44 ) {
44 - connection.socket.on(typeName, (message: T) => { 45 + connection.socket.on(typeName, (message: T, callback: Function) => {
45 if (connection.user !== undefined) { 46 if (connection.user !== undefined) {
46 - handler(connection.user, message); 47 + const response = handler(connection.user, message);
48 + callback(response);
49 + } else {
50 + callback({ ok: false });
47 } 51 }
48 }); 52 });
49 } 53 }
......
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 { LoginMessage } from "../types"; 4 +import { LoginMessage, MessageResponse } from "../types";
5 5
6 export function loginHandler( 6 export function loginHandler(
7 connection: Connection, 7 connection: Connection,
8 message: LoginMessage 8 message: LoginMessage
9 -): void { 9 +): MessageResponse {
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
13 RoomManager.instance().sendList(connection); 13 RoomManager.instance().sendList(connection);
14 +
15 + return { ok: true };
14 } 16 }
......
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 { RoomChatMessage, RoomJoinMessage } from "../types"; 4 +import { MessageResponse, RoomChatMessage, RoomJoinMessage } from "../types";
5 5
6 -export function roomChatHandler(user: User, message: RoomChatMessage): void { 6 +export function roomChatHandler(
7 + user: User,
8 + message: RoomChatMessage
9 +): MessageResponse {
7 user.room?.sendChat(user, message.message); 10 user.room?.sendChat(user, message.message);
11 + return { ok: true };
8 } 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 { RoomJoinMessage } from "../types"; 4 +import { MessageResponse, RoomJoinMessage } from "../types";
5 5
6 -export function roomJoinHandler(user: User, message: RoomJoinMessage): void { 6 +export function roomJoinHandler(
7 + user: User,
8 + message: RoomJoinMessage
9 +): MessageResponse {
7 const room = RoomManager.instance().get(message.uuid); 10 const room = RoomManager.instance().get(message.uuid);
8 if (room !== undefined) { 11 if (room !== undefined) {
9 room.connect(user); 12 room.connect(user);
13 + return { ok: true };
10 } 14 }
15 + return { ok: false };
11 } 16 }
......
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 { RoomLeaveMessage } from "../types"; 4 +import { MessageResponse, RoomLeaveMessage } from "../types";
5 5
6 -export function roomLeaveHandler(user: User, message: RoomLeaveMessage): void { 6 +export function roomLeaveHandler(
7 + user: User,
8 + message: RoomLeaveMessage
9 +): MessageResponse {
7 user.room?.disconnect(user); 10 user.room?.disconnect(user);
11 + return { ok: true };
8 } 12 }
......
...@@ -6,6 +6,16 @@ export interface Message { ...@@ -6,6 +6,16 @@ export interface Message {
6 } 6 }
7 7
8 /** 8 /**
9 + * 서버에 Event를 보냈을 때 요청에 대한 결과를 전송받습니다.
10 + * @param ok 요청의 성공 여부입니다.
11 + * @param reason 요청 실패 사유입니다. 필요한 경우에만 포함됩니다.
12 + */
13 +export interface MessageResponse {
14 + ok: boolean;
15 + reason?: string;
16 +}
17 +
18 +/**
9 * 클라 -> 서버 19 * 클라 -> 서버
10 * 로그인 정보를 서버에게 전송합니다. 20 * 로그인 정보를 서버에게 전송합니다.
11 */ 21 */
...@@ -77,6 +87,7 @@ export class RoomChatMessage implements Message { ...@@ -77,6 +87,7 @@ export class RoomChatMessage implements Message {
77 } 87 }
78 88
79 export class MessageType { 89 export class MessageType {
90 + static readonly RESPONSE = "response";
80 static readonly LOGIN = "login"; 91 static readonly LOGIN = "login";
81 static readonly ROOM_LIST = "room_list"; 92 static readonly ROOM_LIST = "room_list";
82 static readonly ROOM_JOIN = "room_join"; 93 static readonly ROOM_JOIN = "room_join";
......