Showing
11 changed files
with
76 additions
and
48 deletions
| ... | @@ -17,10 +17,6 @@ export class Connection { | ... | @@ -17,10 +17,6 @@ export class Connection { |
| 17 | this.messageHandlerChain = new MessageHandlerChain(this); | 17 | this.messageHandlerChain = new MessageHandlerChain(this); |
| 18 | } | 18 | } |
| 19 | 19 | ||
| 20 | - public get authenticated(): boolean { | ||
| 21 | - return this.user !== undefined; | ||
| 22 | - } | ||
| 23 | - | ||
| 24 | public send<T extends keyof ServerOutboundMessageMap>( | 20 | public send<T extends keyof ServerOutboundMessageMap>( |
| 25 | type: T, | 21 | type: T, |
| 26 | message: ServerOutboundMessageMap[T] | 22 | message: ServerOutboundMessageMap[T] | ... | ... |
| 1 | import { Connection } from "../connection/Connection"; | 1 | import { Connection } from "../connection/Connection"; |
| 2 | -import { ServerInboundMessageMap, ServerResponse } from "../../common/index"; | 2 | +import { |
| 3 | + ServerInboundMessage, | ||
| 4 | + ServerInboundMessageMap, | ||
| 5 | + ServerResponse, | ||
| 6 | +} from "../../common/index"; | ||
| 3 | 7 | ||
| 4 | type ServerHandlerMap<T> = { | 8 | type ServerHandlerMap<T> = { |
| 5 | [Key in keyof ServerInboundMessageMap]?: ( | 9 | [Key in keyof ServerInboundMessageMap]?: ( |
| 6 | connection: Connection, | 10 | connection: Connection, |
| 7 | - message: Omit<ServerInboundMessageMap[Key], "result">, | 11 | + message: ServerInboundMessage<Key>, |
| 8 | scope: T | 12 | scope: T |
| 9 | - ) => ServerResponse< | 13 | + ) => ServerResponse<Key>; |
| 10 | - "result" extends keyof ServerInboundMessageMap[Key] | ||
| 11 | - ? ServerInboundMessageMap[Key]["result"] | ||
| 12 | - : undefined | ||
| 13 | - >; | ||
| 14 | }; | 14 | }; |
| 15 | 15 | ||
| 16 | export class HandlerMap<T> { | 16 | export class HandlerMap<T> { | ... | ... |
| 1 | import { Connection } from "../connection/Connection"; | 1 | import { Connection } from "../connection/Connection"; |
| 2 | import { ServerInboundMessageMap, ServerResponse } from "../../common/index"; | 2 | import { ServerInboundMessageMap, ServerResponse } from "../../common/index"; |
| 3 | import { keys } from "ts-transformer-keys"; | 3 | import { keys } from "ts-transformer-keys"; |
| 4 | +import { HandlerMap } from "./MessageHandler"; | ||
| 5 | +import { loginHandler } from "./handler/loginHandler"; | ||
| 4 | 6 | ||
| 5 | export class MessageHandlerChain { | 7 | export class MessageHandlerChain { |
| 6 | connection: Connection; | 8 | connection: Connection; |
| 7 | 9 | ||
| 10 | + handler: HandlerMap<undefined>; | ||
| 11 | + | ||
| 8 | constructor(connection: Connection) { | 12 | constructor(connection: Connection) { |
| 9 | this.connection = connection; | 13 | this.connection = connection; |
| 14 | + this.handler = new HandlerMap(undefined, { | ||
| 15 | + login: loginHandler, | ||
| 16 | + }); | ||
| 10 | 17 | ||
| 11 | for (const key in keys<ServerInboundMessageMap>()) { | 18 | for (const key in keys<ServerInboundMessageMap>()) { |
| 12 | const type = key as keyof ServerInboundMessageMap; | 19 | const type = key as keyof ServerInboundMessageMap; |
| 13 | this.connection.socket.on(key, (message: any, callback: Function) => { | 20 | this.connection.socket.on(key, (message: any, callback: Function) => { |
| 14 | if ( | 21 | if ( |
| 15 | - connection?.user && | 22 | + connection?.user?.room && |
| 16 | - connection.user.handler.handle( | 23 | + connection.user.room.handler.handle( |
| 17 | type, | 24 | type, |
| 18 | connection, | 25 | connection, |
| 19 | - message.data, | 26 | + message, |
| 20 | callback | 27 | callback |
| 21 | ) | 28 | ) |
| 22 | ) | 29 | ) |
| 23 | return; | 30 | return; |
| 24 | 31 | ||
| 25 | - // TODO: Add more handlers | 32 | + if ( |
| 33 | + connection?.user && | ||
| 34 | + connection.user.handler.handle(type, connection, message, callback) | ||
| 35 | + ) | ||
| 36 | + return; | ||
| 37 | + | ||
| 38 | + this.handler.handle(type, connection, message, callback); | ||
| 26 | }); | 39 | }); |
| 27 | } | 40 | } |
| 28 | } | 41 | } | ... | ... |
| 1 | +import { ServerInboundMessageMap, ServerResponse } from "../../../common"; | ||
| 1 | import { Connection } from "../../connection/Connection"; | 2 | import { Connection } from "../../connection/Connection"; |
| 2 | import { RoomManager } from "../../room/RoomManager"; | 3 | import { RoomManager } from "../../room/RoomManager"; |
| 3 | import { User } from "../../user/User"; | 4 | import { User } from "../../user/User"; |
| 4 | -import { LoginMessage, MessageResponse } from "../types"; | ||
| 5 | 5 | ||
| 6 | export function loginHandler( | 6 | export function loginHandler( |
| 7 | connection: Connection, | 7 | connection: Connection, |
| 8 | - message: LoginMessage | 8 | + message: ServerInboundMessageMap["login"], |
| 9 | -): MessageResponse<undefined> { | 9 | + scope: undefined |
| 10 | +): ServerResponse<"login"> { | ||
| 10 | connection.user = new User(message.username, connection); | 11 | connection.user = new User(message.username, connection); |
| 11 | console.log(`User ${message.username} has logged in!`); | 12 | console.log(`User ${message.username} has logged in!`); |
| 12 | 13 | ... | ... |
| 1 | +import { ServerInboundMessage, ServerResponse } from "../../../common"; | ||
| 1 | import { Connection } from "../../connection/Connection"; | 2 | import { Connection } from "../../connection/Connection"; |
| 3 | +import { Room } from "../../room/Room"; | ||
| 2 | import { RoomManager } from "../../room/RoomManager"; | 4 | import { RoomManager } from "../../room/RoomManager"; |
| 3 | import { User } from "../../user/User"; | 5 | import { User } from "../../user/User"; |
| 4 | -import { MessageResponse, RoomChatMessage, RoomJoinMessage } from "../types"; | ||
| 5 | 6 | ||
| 6 | -export function roomChatHandler( | 7 | +export function chatHandler( |
| 7 | - user: User, | 8 | + connection: Connection, |
| 8 | - message: RoomChatMessage | 9 | + message: ServerInboundMessage<"chat">, |
| 9 | -): MessageResponse<undefined> { | 10 | + scope: Room |
| 10 | - user.room?.sendChat(user, message.message); | 11 | +): ServerResponse<"chat"> { |
| 12 | + scope.sendChat(user, message.message); | ||
| 11 | return { ok: true }; | 13 | return { ok: true }; |
| 12 | } | 14 | } | ... | ... |
| 1 | +import { ServerInboundMessage, ServerResponse } from "../../../common"; | ||
| 1 | import { Connection } from "../../connection/Connection"; | 2 | import { Connection } from "../../connection/Connection"; |
| 3 | +import { Room } from "../../room/Room"; | ||
| 2 | import { RoomManager } from "../../room/RoomManager"; | 4 | import { RoomManager } from "../../room/RoomManager"; |
| 3 | -import { RoomInfo } from "../../room/types"; | ||
| 4 | import { User } from "../../user/User"; | 5 | import { User } from "../../user/User"; |
| 5 | -import { MessageResponse, RoomJoinMessage } from "../types"; | ||
| 6 | 6 | ||
| 7 | export function roomJoinHandler( | 7 | export function roomJoinHandler( |
| 8 | - user: User, | 8 | + connection: Connection, |
| 9 | - message: RoomJoinMessage | 9 | + message: ServerInboundMessage<"joinRoom">, |
| 10 | -): MessageResponse<RoomInfo> { | 10 | + scope: Room |
| 11 | +): ServerResponse<"joinRoom"> { | ||
| 11 | const room = RoomManager.instance().get(message.uuid); | 12 | const room = RoomManager.instance().get(message.uuid); |
| 12 | if (room !== undefined) { | 13 | if (room !== undefined) { |
| 13 | room.connect(user); | 14 | room.connect(user); | ... | ... |
| 1 | +import { ServerInboundMessage, ServerResponse } from "../../../common"; | ||
| 1 | import { Connection } from "../../connection/Connection"; | 2 | import { Connection } from "../../connection/Connection"; |
| 3 | +import { Room } from "../../room/Room"; | ||
| 2 | import { RoomManager } from "../../room/RoomManager"; | 4 | import { RoomManager } from "../../room/RoomManager"; |
| 3 | import { User } from "../../user/User"; | 5 | import { User } from "../../user/User"; |
| 4 | -import { MessageResponse, RoomLeaveMessage } from "../types"; | ||
| 5 | 6 | ||
| 6 | export function roomLeaveHandler( | 7 | export function roomLeaveHandler( |
| 7 | - user: User, | 8 | + connection: Connection, |
| 8 | - message: RoomLeaveMessage | 9 | + message: ServerInboundMessage<"leaveRoom">, |
| 9 | -): MessageResponse<undefined> { | 10 | + scope: Room |
| 11 | +): ServerResponse<"leaveRoom"> { | ||
| 10 | user.room?.disconnect(user); | 12 | user.room?.disconnect(user); |
| 11 | return { ok: true }; | 13 | return { ok: true }; |
| 12 | } | 14 | } | ... | ... |
| 1 | +import { | ||
| 2 | + ServerInboundMessage, | ||
| 3 | + ServerInboundMessageMap, | ||
| 4 | + ServerResponse, | ||
| 5 | +} from "../../../common"; | ||
| 6 | +import { RoomDescription } from "../../../common/dataType"; | ||
| 1 | import { Connection } from "../../connection/Connection"; | 7 | import { Connection } from "../../connection/Connection"; |
| 2 | import { RoomManager } from "../../room/RoomManager"; | 8 | import { RoomManager } from "../../room/RoomManager"; |
| 3 | -import { RoomDescription, RoomInfo } from "../../room/types"; | ||
| 4 | import { User } from "../../user/User"; | 9 | import { User } from "../../user/User"; |
| 5 | -import { MessageResponse, RoomListRequestMessage } from "../types"; | ||
| 6 | 10 | ||
| 7 | export function roomListRequestHandler( | 11 | export function roomListRequestHandler( |
| 8 | - user: User, | 12 | + connection: Connection, |
| 9 | - message: RoomListRequestMessage | 13 | + message: ServerInboundMessage<"roomList">, |
| 10 | -): MessageResponse<RoomDescription[]> { | 14 | + scope: User |
| 15 | +): ServerResponse<"roomList"> { | ||
| 11 | return { ok: true, result: RoomManager.instance().list() }; | 16 | return { ok: true, result: RoomManager.instance().list() }; |
| 12 | } | 17 | } | ... | ... |
| 1 | import { Connection } from "../connection/Connection"; | 1 | import { Connection } from "../connection/Connection"; |
| 2 | import { v4 as uuidv4 } from "uuid"; | 2 | import { v4 as uuidv4 } from "uuid"; |
| 3 | -import { RoomDescription, RoomInfo } from "./types"; | ||
| 4 | -import { | ||
| 5 | - Message, | ||
| 6 | - RoomChatMessage, | ||
| 7 | - RoomUserUpdateMessage, | ||
| 8 | -} from "../message/types"; | ||
| 9 | -import { UserData } from "../user/types"; | ||
| 10 | import { User } from "../user/User"; | 3 | import { User } from "../user/User"; |
| 4 | +import { MessageHandlerChain } from "../message/MessageHandlerChain"; | ||
| 5 | +import { HandlerMap } from "../message/MessageHandler"; | ||
| 6 | +import { roomJoinHandler } from "../message/handler/roomJoinHandler"; | ||
| 7 | +import { roomLeaveHandler } from "../message/handler/roomLeaveHandler"; | ||
| 8 | +import { chatHandler } from "../message/handler/roomChatHandler"; | ||
| 11 | 9 | ||
| 12 | export class Room { | 10 | export class Room { |
| 13 | public readonly uuid: string; | 11 | public readonly uuid: string; |
| ... | @@ -19,10 +17,17 @@ export class Room { | ... | @@ -19,10 +17,17 @@ export class Room { |
| 19 | 17 | ||
| 20 | private closed: boolean = false; | 18 | private closed: boolean = false; |
| 21 | 19 | ||
| 20 | + public handler: HandlerMap<Room>; | ||
| 21 | + | ||
| 22 | constructor(name: string, maxUsers: number = 8) { | 22 | constructor(name: string, maxUsers: number = 8) { |
| 23 | this.uuid = uuidv4(); | 23 | this.uuid = uuidv4(); |
| 24 | this.name = name; | 24 | this.name = name; |
| 25 | this.maxUsers = maxUsers; | 25 | this.maxUsers = maxUsers; |
| 26 | + this.handler = new HandlerMap<Room>(this, { | ||
| 27 | + joinRoom: roomJoinHandler, | ||
| 28 | + leaveRoom: roomLeaveHandler, | ||
| 29 | + chat: chatHandler, | ||
| 30 | + }); | ||
| 26 | } | 31 | } |
| 27 | 32 | ||
| 28 | public connect(user: User): void { | 33 | public connect(user: User): void { | ... | ... |
| 1 | +import { RoomDescription } from "../../common/dataType"; | ||
| 1 | import { Room } from "./Room"; | 2 | import { Room } from "./Room"; |
| 2 | -import { RoomDescription } from "./types"; | ||
| 3 | 3 | ||
| 4 | export class RoomManager { | 4 | export class RoomManager { |
| 5 | private static _instance: RoomManager; | 5 | private static _instance: RoomManager; | ... | ... |
| 1 | +import { UserData } from "../../common/dataType"; | ||
| 1 | import { Connection } from "../connection/Connection"; | 2 | import { Connection } from "../connection/Connection"; |
| 3 | +import { roomListRequestHandler } from "../message/handler/roomListRequestHandler"; | ||
| 2 | import { HandlerMap } from "../message/MessageHandler"; | 4 | import { HandlerMap } from "../message/MessageHandler"; |
| 3 | import { Room } from "../room/Room"; | 5 | import { Room } from "../room/Room"; |
| 4 | -import { UserData } from "./types"; | ||
| 5 | 6 | ||
| 6 | export class User { | 7 | export class User { |
| 7 | public readonly username: string; | 8 | public readonly username: string; |
| ... | @@ -15,7 +16,9 @@ export class User { | ... | @@ -15,7 +16,9 @@ export class User { |
| 15 | constructor(username: string, connection: Connection) { | 16 | constructor(username: string, connection: Connection) { |
| 16 | this.username = username; | 17 | this.username = username; |
| 17 | this.connection = connection; | 18 | this.connection = connection; |
| 18 | - this.handler = new HandlerMap<User>(this, {}); | 19 | + this.handler = new HandlerMap<User>(this, { |
| 20 | + roomList: roomListRequestHandler, | ||
| 21 | + }); | ||
| 19 | } | 22 | } |
| 20 | 23 | ||
| 21 | public getData(): UserData { | 24 | public getData(): UserData { | ... | ... |
-
Please register or login to post a comment