Showing
8 changed files
with
66 additions
and
55 deletions
| ... | @@ -8,7 +8,6 @@ export class Connection { | ... | @@ -8,7 +8,6 @@ export class Connection { |
| 8 | public readonly socket: Socket; | 8 | public readonly socket: Socket; |
| 9 | 9 | ||
| 10 | public user?: User; | 10 | public user?: User; |
| 11 | - public room?: Room; | ||
| 12 | 11 | ||
| 13 | constructor(socket: Socket) { | 12 | constructor(socket: Socket) { |
| 14 | this.socket = socket; | 13 | this.socket = socket; | ... | ... |
| 1 | import { Connection } from "../connection/Connection"; | 1 | import { Connection } from "../connection/Connection"; |
| 2 | +import { User } from "../user/User"; | ||
| 2 | import { loginHandler } from "./handler/loginHandler"; | 3 | import { loginHandler } from "./handler/loginHandler"; |
| 3 | import { roomJoinHandler } from "./handler/roomJoinHandler"; | 4 | import { roomJoinHandler } from "./handler/roomJoinHandler"; |
| 4 | import { roomLeaveHandler } from "./handler/roomLeaveHandler"; | 5 | import { roomLeaveHandler } from "./handler/roomLeaveHandler"; |
| ... | @@ -7,8 +8,16 @@ import { Message, MessageType } from "./types"; | ... | @@ -7,8 +8,16 @@ import { Message, MessageType } from "./types"; |
| 7 | export class MessageHandlerRegistry { | 8 | export class MessageHandlerRegistry { |
| 8 | static registerHandlers(connection: Connection) { | 9 | static registerHandlers(connection: Connection) { |
| 9 | this.registerHandler(connection, MessageType.LOGIN, loginHandler); | 10 | this.registerHandler(connection, MessageType.LOGIN, loginHandler); |
| 10 | - this.registerHandler(connection, MessageType.ROOM_JOIN, roomJoinHandler); | 11 | + this.registerHandlerAuthed( |
| 11 | - this.registerHandler(connection, MessageType.ROOM_LEAVE, roomLeaveHandler); | 12 | + connection, |
| 13 | + MessageType.ROOM_JOIN, | ||
| 14 | + roomJoinHandler | ||
| 15 | + ); | ||
| 16 | + this.registerHandlerAuthed( | ||
| 17 | + connection, | ||
| 18 | + MessageType.ROOM_LEAVE, | ||
| 19 | + roomLeaveHandler | ||
| 20 | + ); | ||
| 12 | } | 21 | } |
| 13 | 22 | ||
| 14 | private static registerHandler<T extends Message>( | 23 | private static registerHandler<T extends Message>( |
| ... | @@ -20,4 +29,16 @@ export class MessageHandlerRegistry { | ... | @@ -20,4 +29,16 @@ export class MessageHandlerRegistry { |
| 20 | handler(connection, message); | 29 | handler(connection, message); |
| 21 | }); | 30 | }); |
| 22 | } | 31 | } |
| 32 | + | ||
| 33 | + private static registerHandlerAuthed<T extends Message>( | ||
| 34 | + connection: Connection, | ||
| 35 | + typeName: string, | ||
| 36 | + handler: (user: User, message: T) => void | ||
| 37 | + ) { | ||
| 38 | + connection.socket.on(typeName, (message: T) => { | ||
| 39 | + if (connection.user !== undefined) { | ||
| 40 | + handler(connection.user, message); | ||
| 41 | + } | ||
| 42 | + }); | ||
| 43 | + } | ||
| 23 | } | 44 | } | ... | ... |
| ... | @@ -7,7 +7,7 @@ export function loginHandler( | ... | @@ -7,7 +7,7 @@ export function loginHandler( |
| 7 | connection: Connection, | 7 | connection: Connection, |
| 8 | message: LoginMessage | 8 | message: LoginMessage |
| 9 | ): void { | 9 | ): void { |
| 10 | - connection.user = new User(message.username); | 10 | + connection.user = new User(message.username, connection); |
| 11 | console.log(`User ${message.username} has logged in!`); | 11 | console.log(`User ${message.username} has logged in!`); |
| 12 | 12 | ||
| 13 | RoomManager.instance().sendList(connection); | 13 | RoomManager.instance().sendList(connection); | ... | ... |
| 1 | import { Connection } from "../../connection/Connection"; | 1 | import { Connection } from "../../connection/Connection"; |
| 2 | import { RoomManager } from "../../room/RoomManager"; | 2 | import { RoomManager } from "../../room/RoomManager"; |
| 3 | +import { User } from "../../user/User"; | ||
| 3 | import { RoomJoinMessage } from "../types"; | 4 | import { RoomJoinMessage } from "../types"; |
| 4 | 5 | ||
| 5 | -export function roomJoinHandler( | 6 | +export function roomJoinHandler(user: User, message: RoomJoinMessage): void { |
| 6 | - connection: Connection, | ||
| 7 | - message: RoomJoinMessage | ||
| 8 | -): void { | ||
| 9 | const room = RoomManager.instance().get(message.uuid); | 7 | const room = RoomManager.instance().get(message.uuid); |
| 10 | if (room !== undefined) { | 8 | if (room !== undefined) { |
| 11 | - room.connect(connection); | 9 | + room.connect(user); |
| 12 | } | 10 | } |
| 13 | } | 11 | } | ... | ... |
| 1 | import { Connection } from "../../connection/Connection"; | 1 | import { Connection } from "../../connection/Connection"; |
| 2 | import { RoomManager } from "../../room/RoomManager"; | 2 | import { RoomManager } from "../../room/RoomManager"; |
| 3 | +import { User } from "../../user/User"; | ||
| 3 | import { RoomLeaveMessage } from "../types"; | 4 | import { RoomLeaveMessage } from "../types"; |
| 4 | 5 | ||
| 5 | -export function roomLeaveHandler( | 6 | +export function roomLeaveHandler(user: User, message: RoomLeaveMessage): void { |
| 6 | - connection: Connection, | 7 | + user.room?.disconnect(user); |
| 7 | - message: RoomLeaveMessage | ||
| 8 | -): void { | ||
| 9 | - if (connection.room !== undefined) { | ||
| 10 | - connection.room.disconnect(connection); | ||
| 11 | - } | ||
| 12 | } | 8 | } | ... | ... |
| ... | @@ -7,60 +7,50 @@ import { | ... | @@ -7,60 +7,50 @@ import { |
| 7 | RoomUserUpdateMessage, | 7 | RoomUserUpdateMessage, |
| 8 | } from "../message/types"; | 8 | } from "../message/types"; |
| 9 | import { UserData } from "../user/types"; | 9 | import { UserData } from "../user/types"; |
| 10 | +import { User } from "../user/User"; | ||
| 10 | 11 | ||
| 11 | export class Room { | 12 | export class Room { |
| 12 | public readonly uuid: string; | 13 | public readonly uuid: string; |
| 13 | 14 | ||
| 14 | public name: string; | 15 | public name: string; |
| 15 | - public readonly maxConnections: number; | 16 | + public readonly maxUsers: number; |
| 16 | 17 | ||
| 17 | - private connections: Connection[] = []; | 18 | + private users: User[] = []; |
| 18 | 19 | ||
| 19 | private closed: boolean = false; | 20 | private closed: boolean = false; |
| 20 | 21 | ||
| 21 | - constructor(name: string, maxConnections: number = 8) { | 22 | + constructor(name: string, maxUsers: number = 8) { |
| 22 | this.uuid = uuidv4(); | 23 | this.uuid = uuidv4(); |
| 23 | this.name = name; | 24 | this.name = name; |
| 24 | - this.maxConnections = maxConnections; | 25 | + this.maxUsers = maxUsers; |
| 25 | } | 26 | } |
| 26 | 27 | ||
| 27 | - public connect(connection: Connection): void { | 28 | + public connect(user: User): void { |
| 28 | - // TODO: 더 나은 인증 처리 | 29 | + if (this.users.includes(user) || this.users.length >= this.maxUsers) { |
| 29 | - const user = connection.user; | ||
| 30 | - if (user === undefined) { | ||
| 31 | return; | 30 | return; |
| 32 | } | 31 | } |
| 33 | 32 | ||
| 34 | - if ( | 33 | + this.users.push(user); |
| 35 | - this.connections.includes(connection) || | 34 | + user.room = this; // TODO: 더 나은 관리 |
| 36 | - this.connections.length >= this.maxConnections | ||
| 37 | - ) { | ||
| 38 | - return; | ||
| 39 | - } | ||
| 40 | - | ||
| 41 | - this.connections.push(connection); | ||
| 42 | - connection.room = this; // TODO: 더 나은 관리 | ||
| 43 | 35 | ||
| 44 | this.broadcast(new RoomUserUpdateMessage("added", user.getData())); | 36 | this.broadcast(new RoomUserUpdateMessage("added", user.getData())); |
| 45 | 37 | ||
| 46 | var users: UserData[] = []; | 38 | var users: UserData[] = []; |
| 47 | - this.connections.forEach((con) => { | 39 | + this.users.forEach((u) => { |
| 48 | - if (con.user !== undefined && connection !== con) { | 40 | + if (user !== u) { |
| 49 | - users.push(con.user.getData()); | 41 | + users.push(u.getData()); |
| 50 | } | 42 | } |
| 51 | }); | 43 | }); |
| 52 | - connection.send(new RoomInfoMessage(users)); | 44 | + user.connection.send(new RoomInfoMessage(users)); |
| 53 | } | 45 | } |
| 54 | 46 | ||
| 55 | - public disconnect(connection: Connection): void { | 47 | + public disconnect(user: User): void { |
| 56 | - const index = this.connections.indexOf(connection); | 48 | + const index = this.users.indexOf(user); |
| 57 | - if (connection.user !== undefined && index > -1) { | 49 | + if (index > -1) { |
| 58 | - this.connections.splice(index, 1); | 50 | + this.users.splice(index, 1); |
| 59 | - connection.room = undefined; | 51 | + user.room = undefined; |
| 60 | 52 | ||
| 61 | - this.broadcast( | 53 | + this.broadcast(new RoomUserUpdateMessage("removed", user.getData())); |
| 62 | - new RoomUserUpdateMessage("removed", connection.user.getData()) | ||
| 63 | - ); | ||
| 64 | } | 54 | } |
| 65 | } | 55 | } |
| 66 | 56 | ||
| ... | @@ -68,22 +58,22 @@ export class Room { | ... | @@ -68,22 +58,22 @@ export class Room { |
| 68 | return { | 58 | return { |
| 69 | uuid: this.uuid, | 59 | uuid: this.uuid, |
| 70 | name: this.name, | 60 | name: this.name, |
| 71 | - currentConnections: this.connections.length, | 61 | + currentUsers: this.users.length, |
| 72 | - maxConnections: this.maxConnections, | 62 | + maxUsers: this.maxUsers, |
| 73 | }; | 63 | }; |
| 74 | } | 64 | } |
| 75 | 65 | ||
| 76 | - public broadcast(message: Message, except?: Connection): void { | 66 | + public broadcast(message: Message, except?: User): void { |
| 77 | - this.connections.forEach((connection) => { | 67 | + this.users.forEach((u) => { |
| 78 | - if (connection !== except) { | 68 | + if (u !== except) { |
| 79 | - connection.send(message); | 69 | + u.connection.send(message); |
| 80 | } | 70 | } |
| 81 | }); | 71 | }); |
| 82 | } | 72 | } |
| 83 | 73 | ||
| 84 | public close(): void { | 74 | public close(): void { |
| 85 | if (!this.closed) { | 75 | if (!this.closed) { |
| 86 | - this.connections.forEach((connection) => this.disconnect(connection)); | 76 | + this.users.forEach((u) => this.disconnect(u)); |
| 87 | this.closed = true; | 77 | this.closed = true; |
| 88 | } | 78 | } |
| 89 | } | 79 | } | ... | ... |
| 1 | +import { Connection } from "../connection/Connection"; | ||
| 2 | +import { Room } from "../room/Room"; | ||
| 1 | import { UserData } from "./types"; | 3 | import { UserData } from "./types"; |
| 2 | 4 | ||
| 3 | export class User { | 5 | export class User { |
| 4 | public readonly username: string; | 6 | public readonly username: string; |
| 5 | 7 | ||
| 6 | - constructor(username: string) { | 8 | + public readonly connection: Connection; |
| 9 | + | ||
| 10 | + public room?: Room; | ||
| 11 | + | ||
| 12 | + constructor(username: string, connection: Connection) { | ||
| 7 | this.username = username; | 13 | this.username = username; |
| 14 | + this.connection = connection; | ||
| 8 | } | 15 | } |
| 9 | 16 | ||
| 10 | public getData(): UserData { | 17 | public getData(): UserData { | ... | ... |
-
Please register or login to post a comment