import express from "express" ; import { Server } from "net" ; import path from "path" ; import { IClient } from "./models/client" ; import { IMessage } from "./models/message" ; import { Realm } from "./models/realm" ; import { IRealm } from "./models/realm" ; import { CheckBrokenConnections } from "./services/checkBrokenConnections" ; import { IMessagesExpire , MessagesExpire } from "./services/messagesExpire" ; import { IWebSocketServer , WebSocketServer } from "./services/webSocketServer" ; import { MessageHandler } from "./messageHandler" ; import { Api } from "./api" ; import { IConfig } from "./config" ; export const createInstance = ( { app , server , options } ) : void => { const config = options ; const realm = new Realm ( ) ; const messageHandler = new MessageHandler ( realm ) ; const api = Api ( { config , realm , messageHandler } ) ; const messagesExpire = new MessagesExpire ( { realm , config , messageHandler } ) ; const checkBrokenConnections = new CheckBrokenConnections ( { realm , config , onClose : client => { app . emit ( "disconnect" , client ) ; } } ) ; app . use ( options . path , api ) ; const customConfig = { ... config , path : path . posix . join ( app . path ( ) , options . path , '/' ) } ; const wss = new WebSocketServer ( { server , realm , config : customConfig } ) ; wss . on ( "connection" , ( client ) => { const messageQueue = realm . getMessageQueueById ( client . getId ( ) ) ; if ( messageQueue ) { let message ; while ( ( message = messageQueue . readMessage ( ) ) ) { messageHandler . handle ( client , message ) ; } realm . clearMessageQueue ( client . getId ( ) ) ; } app . emit ( "connection" , client ) ; } ) ; wss . on ( "message" , ( client , message ) => { app . emit ( "message" , client , message ) ; messageHandler . handle ( client , message ) ; } ) ; wss . on ( "close" , ( client ) => { app . emit ( "disconnect" , client ) ; } ) ; wss . on ( "error" , ( error ) => { app . emit ( "error" , error ) ; } ) ; messagesExpire . startMessagesExpiration ( ) ; checkBrokenConnections . start ( ) ; } ;