delphirtl • Docs
delphirtl / BaseJsonRpcServer
This is a JSON RPC server that conforms to the JSON RPC 2.0 spec as documented at https://www.jsonrpc.org/specification
BaseJsonRpcServer
new BaseJsonRpcServer(
host
?,port
?):BaseJsonRpcServer
Constructs a BaseJsonRpcServer server and returns it. Listens on localhost:8080 by default, on both IPv4 and IPv6 To listen only on IPv4, override onBeforeListening and call dns.setDefaultResultOrder(‘ipv4first’); To listen only on IPv6, if both IPv4 and IPv6 is enabled, just pass “localhost” to the host parameter
• host?: string
= "::"
The IP address/hostname to listen on
• port?: number
= CDefaultPort
The port number to listen on
BaseJsonRpcServer
protected
mExpressServer:Express
protected
mHttpTerminator:undefined
|HttpTerminator
protected
mJsonRpcServer:JSONRPCServer
<void
>
protected
mListeningHost:string
protected
mListeningPort:number
protected
mRequest:undefined
|Request
<ParamsDictionary
,any
,any
,ParsedQs
,Record
<string
,any
>>
Only valid when processing a request
protected
mResponse:undefined
|Response
<any
,Record
<string
,any
>>
Only valid when processing a request
protected
mServer:undefined
|Server
get listeningPath():
string
Returns the path on which to listen to requests for. Override to listen on another path.
string
path on which to listen to requests for
protected
addRPCMethods():void
Adds all necessary JSON RPC methods to this.mJsonRpcServer
void
protected
afterConstruction():void
Allows descendants to do something after class construction
void
void
protected
createInvalidParams(data
?):JSONRPCErrorException
Creates an Invalid Params JSON RPC Error exception, with the message as Invalid Params. The caller must throw the return of this call.
• data?: any
Invalid Params JSON RPC Error exception
protected
createInvalidParamsMessage(message
,data
?):JSONRPCErrorException
Creates an Invalid Params JSON RPC Error exception, with the given message. The caller must throw the return of this call.
• message: string
• data?: any
Invalid Params JSON RPC Error exception
protected
createInvalidRequest(data
?):JSONRPCErrorException
Creates an Invalid Request JSON RPC Error exception, with the message as Invalid Params. The caller must throw the return of this call.
• data?: any
Invalid Request JSON RPC Error exception
protected
createInvalidRequestResponse(request
):JSONRPCErrorResponse
Creates an Invalid Request JSON RPC Error response.
• request: any
JSONRPCErrorResponse
Invalid Params JSON RPC response
protected
createMethodNotFoundResponse(request
):JSONRPCErrorException
Creates a Method not found JSON RPC Error response, with the message as Invalid Params. The caller must throw the return of this call.
• request: any
Invalid Request JSON RPC Error exception
protected
doParseError(err
,req
,res
,next
):void
Calls this.onParseError If onParseError doesn’t handle the error, doParseError will send a JSON RPC Error response if the error is a parsing error, or pass the error up to the next middleware to handle
• err: any
• req: Request
• res: Response
• next: NextFunction
void
Invalid Request JSON RPC Error exception
echo(
params
):any
• params: any
any
protected
handleRequest(request
,response
):Promise
<void
>
Handles a single or a batch JSON RPC request.
• request: Request
<ParamsDictionary
, any
, any
, ParsedQs
, Record
<string
, any
>>
• response: Response
<any
, Record
<string
, any
>>
Promise
<void
>
protected
implementedRPCMethods():SimpleJSONRPCMethod
<void
>[]
Returns RPC methods implemented by the server Override to return an array of RPC methods implemented.
SimpleJSONRPCMethod
<void
>[]
array of RPC methods implemented
protected
initExpress():Express
Initializes the Express server and returns it
Express
protected
initJsonRpcServer():JSONRPCServer
<void
>
Initializes the JSON RPC server and returns it
JSONRPCServer
<void
>
protected
initParseErrorHandler(aExpress
):void
Returns a JSON parse error during express.json parsing
• aExpress: Express
void
protected
JsonRpcErrorListener(message
,data
):void
Error listener for JSON RPC, override to handle. Called when an error occurred during the JSON RPC method call. Do not throw an exception within this method
• message: string
• data: unknown
void
listen(
port
?):Promise
<void
>
Starts listening for RPC requests
• port?: number
Promise
<void
>
protected
log(…data
):void
• …data: any
[]
void
protected
logListeningMethods(methods
):void
Displays the JSON RPC methods being listened to
• methods: SimpleJSONRPCMethod
<void
>[]
void
protected
onBeforeDispatchRequest(next
,request
,serverParams
):JSONRPCResponsePromise
Called before every valid JSON body is dispatched, this doesn’t mean that the request is a valid JSON RPC call though. If the request is valid, the method should call next(request, serverParams) and return its result. If the request is invalid, it should throw an appropriate error.
• next: JSONRPCServerMiddlewareNext
<void
>
• request: JSONRPCRequest
• serverParams: undefined
| void
protected
onBeforeListening():void
This is called before the JSON RPC server starts listening on the port Override this, for example, to listen just on IPv4 by calling dns.setDefaultResultOrder(‘ipv4first’);
void
protected
onParseError(err
,request
,response
):void
Called when there’s a parsing error. The overridden method should create a JSON RPC Error response and send it using response.send(err_response) If the error is not handled, this class will return an error
• err: any
• request: Request
• response: TSendHandler
void
protected
sendInvalidParams(data
?):void
Creates an Invalid Params JSON RPC Error exception and sends it.
• data?: any
void
protected
sendInvalidRequest(data
?):void
Creates an Invalid Request JSON RPC Error exception and sends it.
• data?: any
void
stop():
Promise
<void
>
Stops the RPC server. This can be called by a RPC call or by the app itself
Promise
<void
>
protected
stopListening():Promise
<void
>
Promise
<void
>
protected
waitForServerListening():Promise
<void
>
Waits for the server to starts listening before returning. Override to disable this
Promise
<void
>
protected
waitForServerStopListening():Promise
<void
>
Promise
<void
>