import { MessageType } from "../enums" ; import { IClient } from "../models/client" ; import { IMessage } from "../models/message" ; import { IRealm } from "../models/realm" ; import { Handler } from "./handler" ; import { HeartbeatHandler , TransmissionHandler } from "./handlers" ; import { IHandlersRegistry , HandlersRegistry } from "./handlersRegistry" ; export interface IMessageHandler { handle ( client , message ) : boolean ; } export class MessageHandler implements IMessageHandler { constructor ( realm , private readonly handlersRegistry = new HandlersRegistry ( ) ) { const transmissionHandler = TransmissionHandler ( { realm } ) ; const heartbeatHandler = HeartbeatHandler ; const handleTransmission = ( client , { type , src , dst , payload } ) : boolean => { return transmissionHandler ( client , { type , src , dst , payload , } ) ; } ; const handleHeartbeat = ( client , message ) => heartbeatHandler ( client , message ) ; this . handlersRegistry . registerHandler ( MessageType . HEARTBEAT , handleHeartbeat ) ; this . handlersRegistry . registerHandler ( MessageType . OFFER , handleTransmission ) ; this . handlersRegistry . registerHandler ( MessageType . ANSWER , handleTransmission ) ; this . handlersRegistry . registerHandler ( MessageType . CANDIDATE , handleTransmission ) ; this . handlersRegistry . registerHandler ( MessageType . LEAVE , handleTransmission ) ; this . handlersRegistry . registerHandler ( MessageType . EXPIRE , handleTransmission ) ; } public handle ( client , message ) { return this . handlersRegistry . handle ( client , message ) ; } }