/**
 * app class
 */ import * as path from "path" ; import { I_someConfig , loggerType , I_clientSocket , I_connectorConfig , I_encodeDecodeConfig , I_rpcConfig , ServerInfo , loggerLevel } from "./util/interfaceDefine" ; import * as appUtil from "./util/appUtil" ; import { EventEmitter } from "events" ; import { RpcSocketPool } from "./components/rpcSocketPool" ; import { FrontendServer } from "./components/frontendServer" ; import { BackendServer } from "./components/backendServer" ; import { Session } from "./components/session" ; declare global { interface Rpc { } } export default class Application extends EventEmitter { appName : string = "hello world" ; hasStarted : boolean = false ; main : string = "" ; base : string = path . dirname ( ( require . main as any ) . filename ) ; routeConfig : string [ ] = [ ] ; masterConfig : ServerInfo = { } as ServerInfo ; serversConfig : { [ serverType ] : ServerInfo [ ] ; } = { } ; routeConfig2 : string [ ] [ ] = [ ] ; clientNum : number = 0 ; clients : { [ uid ] : I_clientSocket ; } = { } ; settings : { [ key ] : any ; } = { } ; servers : { [ serverType ] : ServerInfo [ ] ; } = { } ; serversIdMap : { [ id ] : ServerInfo ; } = { } ; serverInfo : ServerInfo = { } as ServerInfo ; isDaemon : boolean = false ; env : string = "" ; serverId : string = "" ; serverType : string = "" ; frontend : boolean = false ; startMode : "all" | "alone" = "all" ; startTime : number = 0 ; router : { [ serverType ] : ( session : Session ) => string ; } = { } ; rpc : ( serverId : string ) => Rpc = null as any ; rpcAwait : ( serverId : string , notify : boolean ) => Rpc = null as any ; rpcPool : RpcSocketPool = new RpcSocketPool ( ) ; logger : ( type : loggerType , level : loggerLevel , msg : string ) => void = function ( ) { } ; msgEncode : Required < I_encodeDecodeConfig > [ "msgEncode" ] = null as any ; msgDecode : Required < I_encodeDecodeConfig > [ "msgDecode" ] = null as any ; protoEncode : Required < I_encodeDecodeConfig > [ "protoEncode" ] = null as any ; protoDecode : Required < I_encodeDecodeConfig > [ "protoDecode" ] = null as any ; someconfig : I_someConfig = { } as any ; noRpcMatrix : { [ svrT_svrT ] : boolean ; } = { } ; frontendServer : FrontendServer = null as any ; backendServer : BackendServer = null as any ; constructor ( ) { super ( ) ; appUtil . defaultConfiguration ( this ) ; } /**
     * Start up
     */ start ( ) { if ( this . hasStarted ) { console . error ( "the app has already started" ) ; return ; } this . hasStarted = true ; this . startTime = new Date ( ) . getTime ( ) ; appUtil . startServer ( this ) ; } setConfig ( key , value ) { this . someconfig [ key ] = value ; if ( key === "logger" ) { this . logger = value ; } else if ( key === "rpc" ) { let noRpcMatrix = value [ "noRpcMatrix" ] || { } ; for ( let svrT1 in noRpcMatrix ) { let arr = noRpcMatrix [ svrT1 ] ; for ( let svrT2 of arr ) { this . noRpcMatrix [ appUtil . getNoRpcKey ( svrT1 , svrT2 ) ] = true ; } } } } /**
     * Set key-value pairs
     */ set ( key , value ) { this . settings [ key ] = value ; return value ; } /**
     * Get the value corresponding to the key
     */ get ( key ) { return this . settings [ key ] ; } /**
     * Delete a key-value pair
     */ delete ( key ) { delete this . settings [ key ] ; } /**
     * Get the server array according to the server type
     */ getServersByType ( serverType ) { return this . servers [ serverType ] || [ ] ; } /**
     * Get a server configuration
     */ getServerById ( serverId ) { return this . serversIdMap [ serverId ] ; } param serverType param routeFunc route ( serverType , routeFunc ) { this . router [ serverType ] = routeFunc ; } /**
     * get session by uid
     */ getSession ( uid ) { let client = this . clients [ uid ] ; if ( client ) { return client . session ; } else { return null ; } } /**
     * get all clients
     */ getAllClients ( ) { return this . clients ; } param cmd param msg param uids sendMsgByUid ( cmd , msg , uids ) { if ( msg === undefined ) { msg = null ; } let msgBuf = this . protoEncode ( cmd , msg ) ; let client ; let i ; for ( i = 0 ; i < uids . length ; i ++ ) { client = this . clients [ uids [ i ] ] ; if ( client ) { client . send ( msgBuf ) ; } } } param cmd param msg sendAll ( cmd , msg ) { if ( msg === undefined ) { msg = null ; } let data = this . protoEncode ( cmd , msg ) ; let uid ; for ( uid in this . clients ) { this . clients [ uid ] . send ( data ) ; } } param cmd param msg param uidsid sendMsgByUidSid ( cmd , msg , uidsid ) { if ( msg === undefined ) { msg = null ; } this . backendServer . sendMsgByUidSid ( cmd , msg , uidsid ) ; } param cmd param msg param group { sid sendMsgByGroup ( cmd , msg , group ) { if ( msg === undefined ) { msg = null ; } this . backendServer . sendMsgByGroup ( cmd , msg , group ) ; } param type param cb configure ( type , cb ) { if ( type === "all" ) { cb . call ( this ) ; return ; } let ts = type . split ( "|" ) ; for ( let i = 0 ; i < ts . length ; i ++ ) { if ( this . serverType === ts [ i ] . trim ( ) ) { cb . call ( this ) ; break ; } } } }