Connection.ts 815 Bytes
import { Socket } from "socket.io";
import { ServerOutboundMessageMap } from "../../common";
import { loginHandler } from "../message/handler/loginHandler";
import { MessageHandlerChain } from "../message/MessageHandlerChain";
import { Room } from "../room/Room";
import { User } from "../user/User";

export class Connection {
  public readonly socket: Socket;

  public user?: User;

  private messageHandlerChain: MessageHandlerChain;

  constructor(socket: Socket) {
    this.socket = socket;
    this.messageHandlerChain = new MessageHandlerChain(this);
  }

  public get authenticated(): boolean {
    return this.user !== undefined;
  }

  public send<T extends keyof ServerOutboundMessageMap>(
    type: T,
    message: ServerOutboundMessageMap[T]
  ) {
    this.socket.emit(type as string, message);
  }
}