Connection.ts 727 Bytes
import { Socket } from "socket.io";
import { ServerOutboundMessage, ServerOutboundMessageKey } from "../../common";
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 send<T extends ServerOutboundMessageKey>(
    type: T,
    message: ServerOutboundMessage<T>
  ) {
    this.socket.emit("msg", {
      type: type as string,
      message: message,
    });
  }
}