강동현

핸들러 수정 및 등록

......@@ -17,10 +17,6 @@ export class Connection {
this.messageHandlerChain = new MessageHandlerChain(this);
}
public get authenticated(): boolean {
return this.user !== undefined;
}
public send<T extends keyof ServerOutboundMessageMap>(
type: T,
message: ServerOutboundMessageMap[T]
......
import { Connection } from "../connection/Connection";
import { ServerInboundMessageMap, ServerResponse } from "../../common/index";
import {
ServerInboundMessage,
ServerInboundMessageMap,
ServerResponse,
} from "../../common/index";
type ServerHandlerMap<T> = {
[Key in keyof ServerInboundMessageMap]?: (
connection: Connection,
message: Omit<ServerInboundMessageMap[Key], "result">,
message: ServerInboundMessage<Key>,
scope: T
) => ServerResponse<
"result" extends keyof ServerInboundMessageMap[Key]
? ServerInboundMessageMap[Key]["result"]
: undefined
>;
) => ServerResponse<Key>;
};
export class HandlerMap<T> {
......
import { Connection } from "../connection/Connection";
import { ServerInboundMessageMap, ServerResponse } from "../../common/index";
import { keys } from "ts-transformer-keys";
import { HandlerMap } from "./MessageHandler";
import { loginHandler } from "./handler/loginHandler";
export class MessageHandlerChain {
connection: Connection;
handler: HandlerMap<undefined>;
constructor(connection: Connection) {
this.connection = connection;
this.handler = new HandlerMap(undefined, {
login: loginHandler,
});
for (const key in keys<ServerInboundMessageMap>()) {
const type = key as keyof ServerInboundMessageMap;
this.connection.socket.on(key, (message: any, callback: Function) => {
if (
connection?.user &&
connection.user.handler.handle(
connection?.user?.room &&
connection.user.room.handler.handle(
type,
connection,
message.data,
message,
callback
)
)
return;
// TODO: Add more handlers
if (
connection?.user &&
connection.user.handler.handle(type, connection, message, callback)
)
return;
this.handler.handle(type, connection, message, callback);
});
}
}
......
import { ServerInboundMessageMap, ServerResponse } from "../../../common";
import { Connection } from "../../connection/Connection";
import { RoomManager } from "../../room/RoomManager";
import { User } from "../../user/User";
import { LoginMessage, MessageResponse } from "../types";
export function loginHandler(
connection: Connection,
message: LoginMessage
): MessageResponse<undefined> {
message: ServerInboundMessageMap["login"],
scope: undefined
): ServerResponse<"login"> {
connection.user = new User(message.username, connection);
console.log(`User ${message.username} has logged in!`);
......
import { ServerInboundMessage, ServerResponse } from "../../../common";
import { Connection } from "../../connection/Connection";
import { Room } from "../../room/Room";
import { RoomManager } from "../../room/RoomManager";
import { User } from "../../user/User";
import { MessageResponse, RoomChatMessage, RoomJoinMessage } from "../types";
export function roomChatHandler(
user: User,
message: RoomChatMessage
): MessageResponse<undefined> {
user.room?.sendChat(user, message.message);
export function chatHandler(
connection: Connection,
message: ServerInboundMessage<"chat">,
scope: Room
): ServerResponse<"chat"> {
scope.sendChat(user, message.message);
return { ok: true };
}
......
import { ServerInboundMessage, ServerResponse } from "../../../common";
import { Connection } from "../../connection/Connection";
import { Room } from "../../room/Room";
import { RoomManager } from "../../room/RoomManager";
import { RoomInfo } from "../../room/types";
import { User } from "../../user/User";
import { MessageResponse, RoomJoinMessage } from "../types";
export function roomJoinHandler(
user: User,
message: RoomJoinMessage
): MessageResponse<RoomInfo> {
connection: Connection,
message: ServerInboundMessage<"joinRoom">,
scope: Room
): ServerResponse<"joinRoom"> {
const room = RoomManager.instance().get(message.uuid);
if (room !== undefined) {
room.connect(user);
......
import { ServerInboundMessage, ServerResponse } from "../../../common";
import { Connection } from "../../connection/Connection";
import { Room } from "../../room/Room";
import { RoomManager } from "../../room/RoomManager";
import { User } from "../../user/User";
import { MessageResponse, RoomLeaveMessage } from "../types";
export function roomLeaveHandler(
user: User,
message: RoomLeaveMessage
): MessageResponse<undefined> {
connection: Connection,
message: ServerInboundMessage<"leaveRoom">,
scope: Room
): ServerResponse<"leaveRoom"> {
user.room?.disconnect(user);
return { ok: true };
}
......
import {
ServerInboundMessage,
ServerInboundMessageMap,
ServerResponse,
} from "../../../common";
import { RoomDescription } from "../../../common/dataType";
import { Connection } from "../../connection/Connection";
import { RoomManager } from "../../room/RoomManager";
import { RoomDescription, RoomInfo } from "../../room/types";
import { User } from "../../user/User";
import { MessageResponse, RoomListRequestMessage } from "../types";
export function roomListRequestHandler(
user: User,
message: RoomListRequestMessage
): MessageResponse<RoomDescription[]> {
connection: Connection,
message: ServerInboundMessage<"roomList">,
scope: User
): ServerResponse<"roomList"> {
return { ok: true, result: RoomManager.instance().list() };
}
......
import { Connection } from "../connection/Connection";
import { v4 as uuidv4 } from "uuid";
import { RoomDescription, RoomInfo } from "./types";
import {
Message,
RoomChatMessage,
RoomUserUpdateMessage,
} from "../message/types";
import { UserData } from "../user/types";
import { User } from "../user/User";
import { MessageHandlerChain } from "../message/MessageHandlerChain";
import { HandlerMap } from "../message/MessageHandler";
import { roomJoinHandler } from "../message/handler/roomJoinHandler";
import { roomLeaveHandler } from "../message/handler/roomLeaveHandler";
import { chatHandler } from "../message/handler/roomChatHandler";
export class Room {
public readonly uuid: string;
......@@ -19,10 +17,17 @@ export class Room {
private closed: boolean = false;
public handler: HandlerMap<Room>;
constructor(name: string, maxUsers: number = 8) {
this.uuid = uuidv4();
this.name = name;
this.maxUsers = maxUsers;
this.handler = new HandlerMap<Room>(this, {
joinRoom: roomJoinHandler,
leaveRoom: roomLeaveHandler,
chat: chatHandler,
});
}
public connect(user: User): void {
......
import { RoomDescription } from "../../common/dataType";
import { Room } from "./Room";
import { RoomDescription } from "./types";
export class RoomManager {
private static _instance: RoomManager;
......
import { UserData } from "../../common/dataType";
import { Connection } from "../connection/Connection";
import { roomListRequestHandler } from "../message/handler/roomListRequestHandler";
import { HandlerMap } from "../message/MessageHandler";
import { Room } from "../room/Room";
import { UserData } from "./types";
export class User {
public readonly username: string;
......@@ -15,7 +16,9 @@ export class User {
constructor(username: string, connection: Connection) {
this.username = username;
this.connection = connection;
this.handler = new HandlerMap<User>(this, {});
this.handler = new HandlerMap<User>(this, {
roomList: roomListRequestHandler,
});
}
public getData(): UserData {
......