강동현

MessageHandlerRegistry 구현

1 import { Socket } from "socket.io"; 1 import { Socket } from "socket.io";
2 -import { MessageHandlerRegister } from "../message/handler/MessageHandlerRegister"; 2 +import { MessageHandlerRegistry } from "../message/MessageHandlerRegistry";
3 import { User } from "../user/User"; 3 import { User } from "../user/User";
4 4
5 export class Connection { 5 export class Connection {
...@@ -10,7 +10,7 @@ export class Connection { ...@@ -10,7 +10,7 @@ export class Connection {
10 constructor(socket: Socket) { 10 constructor(socket: Socket) {
11 this.socket = socket; 11 this.socket = socket;
12 12
13 - MessageHandlerRegister.registerMessageHandlers(this); 13 + MessageHandlerRegistry.registerHandlers(this);
14 } 14 }
15 15
16 public get authenticated(): boolean { 16 public get authenticated(): boolean {
......
1 +import { Connection } from "../connection/Connection";
2 +import { loginHandler } from "./handler/LoginHandler";
3 +import { MessageType } from "./types";
4 +
5 +export class MessageHandlerRegistry {
6 + static registerHandlers(connection: Connection) {
7 + this.registerHandler(connection, MessageType.LOGIN, loginHandler);
8 + }
9 +
10 + private static registerHandler<T>(
11 + connection: Connection,
12 + typeName: string,
13 + handler: (connection: Connection, message: T) => void
14 + ) {
15 + connection.socket.on(typeName, (message: T) => {
16 + handler(connection, message);
17 + });
18 + }
19 +}
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 { LoginMessage } from "../types"; 3 import { LoginMessage } from "../types";
4 -import { MessageHandler } from "./MessageHandler";
5 4
6 -export class LoginHandler implements MessageHandler<LoginMessage> { 5 +export function loginHandler(
7 - public handle(connection: Connection, message: LoginMessage): void { 6 + connection: Connection,
7 + message: LoginMessage
8 +): void {
8 connection.user = new User(message.username); 9 connection.user = new User(message.username);
9 console.log(`User ${message.username} has logged in!`); 10 console.log(`User ${message.username} has logged in!`);
10 - }
11 } 11 }
......
1 -import { Connection } from "../../connection/Connection";
2 -
3 -export interface MessageHandler<T> {
4 - handle(connection: Connection, message: T): void;
5 -}
1 -import { Connection } from "../../connection/Connection";
2 -import { LoginMessage } from "../types";
3 -import { LoginHandler } from "./LoginHandler";
4 -
5 -export class MessageHandlerRegister {
6 - static registerMessageHandlers(connection: Connection) {
7 - connection.socket.on("login", (message: LoginMessage) => {
8 - new LoginHandler().handle(connection, message);
9 - });
10 - }
11 -}
1 export interface LoginMessage { 1 export interface LoginMessage {
2 username: string; 2 username: string;
3 } 3 }
4 +
5 +export class MessageType {
6 + static readonly LOGIN = "login";
7 +}
......