강동현

MessageResponse 구현

......@@ -4,7 +4,7 @@ import { loginHandler } from "./handler/loginHandler";
import { roomChatHandler } from "./handler/roomChatHandler";
import { roomJoinHandler } from "./handler/roomJoinHandler";
import { roomLeaveHandler } from "./handler/roomLeaveHandler";
import { Message, MessageType } from "./types";
import { Message, MessageResponse, MessageType } from "./types";
export class MessageHandlerRegistry {
static registerHandlers(connection: Connection) {
......@@ -29,21 +29,25 @@ export class MessageHandlerRegistry {
private static registerHandler<T extends Message>(
connection: Connection,
typeName: string,
handler: (connection: Connection, message: T) => void
handler: (connection: Connection, message: T) => MessageResponse
) {
connection.socket.on(typeName, (message: T) => {
handler(connection, message);
connection.socket.on(typeName, (message: T, callback: Function) => {
const response = handler(connection, message);
callback(response);
});
}
private static registerHandlerAuthed<T extends Message>(
connection: Connection,
typeName: string,
handler: (user: User, message: T) => void
handler: (user: User, message: T) => MessageResponse
) {
connection.socket.on(typeName, (message: T) => {
connection.socket.on(typeName, (message: T, callback: Function) => {
if (connection.user !== undefined) {
handler(connection.user, message);
const response = handler(connection.user, message);
callback(response);
} else {
callback({ ok: false });
}
});
}
......
import { Connection } from "../../connection/Connection";
import { RoomManager } from "../../room/RoomManager";
import { User } from "../../user/User";
import { LoginMessage } from "../types";
import { LoginMessage, MessageResponse } from "../types";
export function loginHandler(
connection: Connection,
message: LoginMessage
): void {
): MessageResponse {
connection.user = new User(message.username, connection);
console.log(`User ${message.username} has logged in!`);
RoomManager.instance().sendList(connection);
return { ok: true };
}
......
import { Connection } from "../../connection/Connection";
import { RoomManager } from "../../room/RoomManager";
import { User } from "../../user/User";
import { RoomChatMessage, RoomJoinMessage } from "../types";
import { MessageResponse, RoomChatMessage, RoomJoinMessage } from "../types";
export function roomChatHandler(user: User, message: RoomChatMessage): void {
export function roomChatHandler(
user: User,
message: RoomChatMessage
): MessageResponse {
user.room?.sendChat(user, message.message);
return { ok: true };
}
......
import { Connection } from "../../connection/Connection";
import { RoomManager } from "../../room/RoomManager";
import { User } from "../../user/User";
import { RoomJoinMessage } from "../types";
import { MessageResponse, RoomJoinMessage } from "../types";
export function roomJoinHandler(user: User, message: RoomJoinMessage): void {
export function roomJoinHandler(
user: User,
message: RoomJoinMessage
): MessageResponse {
const room = RoomManager.instance().get(message.uuid);
if (room !== undefined) {
room.connect(user);
return { ok: true };
}
return { ok: false };
}
......
import { Connection } from "../../connection/Connection";
import { RoomManager } from "../../room/RoomManager";
import { User } from "../../user/User";
import { RoomLeaveMessage } from "../types";
import { MessageResponse, RoomLeaveMessage } from "../types";
export function roomLeaveHandler(user: User, message: RoomLeaveMessage): void {
export function roomLeaveHandler(
user: User,
message: RoomLeaveMessage
): MessageResponse {
user.room?.disconnect(user);
return { ok: true };
}
......
......@@ -6,6 +6,16 @@ export interface Message {
}
/**
* 서버에 Event를 보냈을 때 요청에 대한 결과를 전송받습니다.
* @param ok 요청의 성공 여부입니다.
* @param reason 요청 실패 사유입니다. 필요한 경우에만 포함됩니다.
*/
export interface MessageResponse {
ok: boolean;
reason?: string;
}
/**
* 클라 -> 서버
* 로그인 정보를 서버에게 전송합니다.
*/
......@@ -77,6 +87,7 @@ export class RoomChatMessage implements Message {
}
export class MessageType {
static readonly RESPONSE = "response";
static readonly LOGIN = "login";
static readonly ROOM_LIST = "room_list";
static readonly ROOM_JOIN = "room_join";
......