강동현

RoomChatMessage 구현

1 import { Connection } from "../connection/Connection"; 1 import { Connection } from "../connection/Connection";
2 import { User } from "../user/User"; 2 import { User } from "../user/User";
3 import { loginHandler } from "./handler/loginHandler"; 3 import { loginHandler } from "./handler/loginHandler";
4 +import { roomChatHandler } from "./handler/roomChatHandler";
4 import { roomJoinHandler } from "./handler/roomJoinHandler"; 5 import { roomJoinHandler } from "./handler/roomJoinHandler";
5 import { roomLeaveHandler } from "./handler/roomLeaveHandler"; 6 import { roomLeaveHandler } from "./handler/roomLeaveHandler";
6 import { Message, MessageType } from "./types"; 7 import { Message, MessageType } from "./types";
...@@ -18,6 +19,11 @@ export class MessageHandlerRegistry { ...@@ -18,6 +19,11 @@ export class MessageHandlerRegistry {
18 MessageType.ROOM_LEAVE, 19 MessageType.ROOM_LEAVE,
19 roomLeaveHandler 20 roomLeaveHandler
20 ); 21 );
22 + this.registerHandlerAuthed(
23 + connection,
24 + MessageType.ROOM_CHAT,
25 + roomChatHandler
26 + );
21 } 27 }
22 28
23 private static registerHandler<T extends Message>( 29 private static registerHandler<T extends Message>(
......
1 +import { Connection } from "../../connection/Connection";
2 +import { RoomManager } from "../../room/RoomManager";
3 +import { User } from "../../user/User";
4 +import { RoomChatMessage, RoomJoinMessage } from "../types";
5 +
6 +export function roomChatHandler(user: User, message: RoomChatMessage): void {
7 + user.room?.sendChat(user, message.message);
8 +}
...@@ -65,6 +65,17 @@ export class RoomUserUpdateMessage implements Message { ...@@ -65,6 +65,17 @@ export class RoomUserUpdateMessage implements Message {
65 ) {} 65 ) {}
66 } 66 }
67 67
68 +/**
69 + * 클라 <-> 서버
70 + * 접속한 방에서 채팅을 보내거나 받을 때 사용됩니다. 자신이 보낸 채팅은 서버에 의해 수신되지 않습니다.
71 + * @param message 메세지 내용입니다.
72 + * @param sender 채팅을 보낸 유저의 username입니다. 채팅이 클라이언트로 수신 될 경우에만 값을 가집니다.
73 + */
74 +export class RoomChatMessage implements Message {
75 + readonly type = MessageType.ROOM_CHAT;
76 + constructor(public message: string, public sender?: string) {}
77 +}
78 +
68 export class MessageType { 79 export class MessageType {
69 static readonly LOGIN = "login"; 80 static readonly LOGIN = "login";
70 static readonly ROOM_LIST = "room_list"; 81 static readonly ROOM_LIST = "room_list";
...@@ -72,4 +83,5 @@ export class MessageType { ...@@ -72,4 +83,5 @@ export class MessageType {
72 static readonly ROOM_LEAVE = "room_leave"; 83 static readonly ROOM_LEAVE = "room_leave";
73 static readonly ROOM_INFO = "room_info"; 84 static readonly ROOM_INFO = "room_info";
74 static readonly ROOM_USER_UPDATE = "room_user_update"; 85 static readonly ROOM_USER_UPDATE = "room_user_update";
86 + static readonly ROOM_CHAT = "room_chat";
75 } 87 }
......
...@@ -3,6 +3,7 @@ import { v4 as uuidv4 } from "uuid"; ...@@ -3,6 +3,7 @@ import { v4 as uuidv4 } from "uuid";
3 import { RoomData } from "./types"; 3 import { RoomData } from "./types";
4 import { 4 import {
5 Message, 5 Message,
6 + RoomChatMessage,
6 RoomInfoMessage, 7 RoomInfoMessage,
7 RoomUserUpdateMessage, 8 RoomUserUpdateMessage,
8 } from "../message/types"; 9 } from "../message/types";
...@@ -54,6 +55,10 @@ export class Room { ...@@ -54,6 +55,10 @@ export class Room {
54 } 55 }
55 } 56 }
56 57
58 + public sendChat(user: User, message: string): void {
59 + this.broadcast(new RoomChatMessage(message, user.username), user);
60 + }
61 +
57 public getData(): RoomData { 62 public getData(): RoomData {
58 return { 63 return {
59 uuid: this.uuid, 64 uuid: this.uuid,
......