강동현

MessageResponse에 응답 결과 데이터를 담을 수 있도록 변경

......@@ -26,10 +26,13 @@ export class MessageHandlerRegistry {
);
}
private static registerHandler<T extends Message>(
private static registerHandler<
T extends Message,
S extends Message | undefined
>(
connection: Connection,
typeName: string,
handler: (connection: Connection, message: T) => MessageResponse
handler: (connection: Connection, message: T) => MessageResponse<S>
) {
connection.socket.on(typeName, (message: T, callback: Function) => {
const response = handler(connection, message);
......@@ -37,10 +40,13 @@ export class MessageHandlerRegistry {
});
}
private static registerHandlerAuthed<T extends Message>(
private static registerHandlerAuthed<
T extends Message,
S extends Message | undefined
>(
connection: Connection,
typeName: string,
handler: (user: User, message: T) => MessageResponse
handler: (user: User, message: T) => MessageResponse<S>
) {
connection.socket.on(typeName, (message: T, callback: Function) => {
if (connection.user !== undefined) {
......
......@@ -6,7 +6,7 @@ import { LoginMessage, MessageResponse } from "../types";
export function loginHandler(
connection: Connection,
message: LoginMessage
): MessageResponse {
): MessageResponse<undefined> {
connection.user = new User(message.username, connection);
console.log(`User ${message.username} has logged in!`);
......
......@@ -6,7 +6,7 @@ import { MessageResponse, RoomChatMessage, RoomJoinMessage } from "../types";
export function roomChatHandler(
user: User,
message: RoomChatMessage
): MessageResponse {
): MessageResponse<undefined> {
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 { MessageResponse, RoomJoinMessage } from "../types";
import { MessageResponse, RoomInfoMessage, RoomJoinMessage } from "../types";
export function roomJoinHandler(
user: User,
message: RoomJoinMessage
): MessageResponse {
): MessageResponse<RoomInfoMessage> {
const room = RoomManager.instance().get(message.uuid);
if (room !== undefined) {
room.connect(user);
return { ok: true };
const roomInfoMessage = room.connect(user);
return { ok: roomInfoMessage !== undefined, result: roomInfoMessage };
}
return { ok: false };
}
......
......@@ -6,7 +6,7 @@ import { MessageResponse, RoomLeaveMessage } from "../types";
export function roomLeaveHandler(
user: User,
message: RoomLeaveMessage
): MessageResponse {
): MessageResponse<undefined> {
user.room?.disconnect(user);
return { ok: true };
}
......
......@@ -10,9 +10,10 @@ export interface Message {
* @param ok 요청의 성공 여부입니다.
* @param reason 요청 실패 사유입니다. 필요한 경우에만 포함됩니다.
*/
export interface MessageResponse {
export interface MessageResponse<T> {
ok: boolean;
reason?: string;
result?: T;
}
/**
......@@ -87,7 +88,6 @@ 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";
......
......@@ -26,23 +26,23 @@ export class Room {
this.maxUsers = maxUsers;
}
public connect(user: User): void {
public connect(user: User): RoomInfoMessage | undefined {
if (this.users.includes(user) || this.users.length >= this.maxUsers) {
return;
return undefined;
}
this.broadcast(new RoomUserUpdateMessage("added", user.getData()));
this.users.push(user);
user.room = this; // TODO: 더 나은 관리
this.broadcast(new RoomUserUpdateMessage("added", user.getData()));
var users: UserData[] = [];
this.users.forEach((u) => {
if (user !== u) {
users.push(u.getData());
}
});
user.connection.send(new RoomInfoMessage(users));
return new RoomInfoMessage(users);
}
public disconnect(user: User): void {
......