강동현

RoomChatMessage 구현

import { Connection } from "../connection/Connection";
import { User } from "../user/User";
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";
......@@ -18,6 +19,11 @@ export class MessageHandlerRegistry {
MessageType.ROOM_LEAVE,
roomLeaveHandler
);
this.registerHandlerAuthed(
connection,
MessageType.ROOM_CHAT,
roomChatHandler
);
}
private static registerHandler<T extends Message>(
......
import { Connection } from "../../connection/Connection";
import { RoomManager } from "../../room/RoomManager";
import { User } from "../../user/User";
import { RoomChatMessage, RoomJoinMessage } from "../types";
export function roomChatHandler(user: User, message: RoomChatMessage): void {
user.room?.sendChat(user, message.message);
}
......@@ -65,6 +65,17 @@ export class RoomUserUpdateMessage implements Message {
) {}
}
/**
* 클라 <-> 서버
* 접속한 방에서 채팅을 보내거나 받을 때 사용됩니다. 자신이 보낸 채팅은 서버에 의해 수신되지 않습니다.
* @param message 메세지 내용입니다.
* @param sender 채팅을 보낸 유저의 username입니다. 채팅이 클라이언트로 수신 될 경우에만 값을 가집니다.
*/
export class RoomChatMessage implements Message {
readonly type = MessageType.ROOM_CHAT;
constructor(public message: string, public sender?: string) {}
}
export class MessageType {
static readonly LOGIN = "login";
static readonly ROOM_LIST = "room_list";
......@@ -72,4 +83,5 @@ export class MessageType {
static readonly ROOM_LEAVE = "room_leave";
static readonly ROOM_INFO = "room_info";
static readonly ROOM_USER_UPDATE = "room_user_update";
static readonly ROOM_CHAT = "room_chat";
}
......
......@@ -3,6 +3,7 @@ import { v4 as uuidv4 } from "uuid";
import { RoomData } from "./types";
import {
Message,
RoomChatMessage,
RoomInfoMessage,
RoomUserUpdateMessage,
} from "../message/types";
......@@ -54,6 +55,10 @@ export class Room {
}
}
public sendChat(user: User, message: string): void {
this.broadcast(new RoomChatMessage(message, user.username), user);
}
public getData(): RoomData {
return {
uuid: this.uuid,
......