setExporters

This commit is contained in:
2020-03-05 15:28:02 +01:00
parent ef5a83f1d4
commit 0057625800
3 changed files with 62 additions and 12 deletions
+17 -2
View File
@@ -6,6 +6,9 @@ import bsock = require('bsock');
import * as T from './Types';
import * as U from './Utils';
import * as I from './Interfaces';
import { Socket } from 'dgram';
type Exporters<InterfaceT extends T.RPCInterface = T.RPCInterface, SubResType = {}> = I.RPCExporter<T.RPCInterface<InterfaceT>, keyof InterfaceT, SubResType>[]
/**
* A Websocket-server-on-steroids with built-in RPC capabilities
@@ -31,10 +34,9 @@ export class RPCServer<
*/
constructor(
private port:number,
private exporters: I.RPCExporter<T.RPCInterface<InterfaceT>, keyof InterfaceT, SubResType>[] = [],
private exporters: Exporters<InterfaceT, SubResType> = [],
conf: T.ServerConf = {}
){
if(!conf.visibility) this.visibility = "127.0.0.1"
if(conf.sesame){
@@ -91,6 +93,19 @@ export class RPCServer<
]
}
/**
* Publishes a new list of Exporters. This destroys and restarts the socket
* @param exporters the exporters to publish
*/
public setExporters(exporters: Exporters<InterfaceT, SubResType>):any{
exporters.forEach(U.fixNames)
this.destroy()
this.ws = http.createServer()
this.io = bsock.createServer()
this.exporters = exporters
this.startWebsocket()
}
destroy(): void {
this.io.close()
this.ws.close()