murpc
Asynchronous RPC protocols for mudb
example
See src/example for a contrived demo which will be explained below. To run the demo
npm i mudo
- cd into
src/example
tsc ./*.ts
mudo --socket websocket
A mudb
instance can have multiple RPC protocols for different sets of behaviors. Naturally each RPC protocol consistes of two sides, one for server and one for client, plus a schema to describe the RPC interfaces (the argument and return).
- 1 RPC protocol schema
- 2 server-side protocol
- 3 client-side protocol
- install
- api
1 RPC protocol schema
So the first step to define an RPC protocol is to specify a protocol schema using muschema
.
Like the protocol to be created, the schema always consists of two sides, the server side and the client side. Each side in turn contains schemas for the RPC interfaces that they implement repectively, under the name of the corresponding function. RPC schemas are created using the MuRPC
function, which takes the schemas of the argument and return.
;; ;; ;
2 server-side protocol
You can define the server side of the RPC protocol by creating and then configuring an instance of MuRPCServer
.
;
3 client-side protocol
The last missing piece is the client side of the RPC protocol. Similarly, you can define it through an instance of MuRPCClient
.
;
table of contents
install
npm i murpc
api
1 types
Purely instructive types used to describle the API:
TableOf<T>
:{ [name:string]:T } | {}
RPCSchema
:[ AnyMuSchema, AnyMuSchema ]
RPCProtocolSchema
:{ server:TableOf<RPCSchema>, client:TableOf<RPCSchema> }
NextFn
:(errorMessage:string|undefined, returnValue?) => undefined
ServerRPCHandler
:(rpcArgument, next:NextFn, client?:MuRemoteRPCClient) => undefined
ClientRPCHandler
:(rpcArgument, next:NextFn) => undefined
CallbackFn
:(returnValue) => undefined
RPCCaller
:(rpcArgument, callback:CallbackFn) => undefined
2 MuRPC(argumentSchema, returnSchema)
Exported from murpc/rpc
, used when creating the RPC protocol schema, to define RPC interfaces in terms of the argument and the return:
argumentSchema:AnyMuSchema
the data type of the argumentreturnSchema:AnyMuSchema
the data type of the return value
3 MuRPCServer(server, schema)
Used to define the server side of an RPC protocol. Exported from murpc/server
, it takes these arguments:
server:MuServer
schema:RPCProtocolSchema
the RPC protocol schema
3.1 clients:MuRemoteRPCClient[]
Mocks of all connected clients, used to initiate RPCs to a specific client
3.2 server:MuServer
The underlying MuServer
object
3.3 schema:RPCProtocolSchema
The RPC protocol schema
3.4 configure(spec)
Registers event handlers specifed in spec
spec:{ rpc, ready?, connect?, disconnect?, close? }
rpc:TableOf<ServerRPCHandler>
an object containing the implementations of the functions to be called by clientsready()
called when the server is launchedconnect(client:MuRemoteRPCClient)
called when a client connectsdisconnect(client:MuRemoteRPCClient)
called when a client disconnectsclose()
called when the server is shut down
4 MuRemoteRPCClient
A MuRemoteRPCClient
is the server-side mock of a connected client
4.1 sessionId:string
A unique session id identifying the client
4.2 rpc:TableOf
A table of RPC initiators each under the name of the corresponding remote function on a client
5 MuRPCClient(client, schema)
Used to define the client side of an RPC protocol. Exported from murpc/client
, it takes these arguments:
client:MuClient
schema:ProtocolSche4ma
the RPC protocol schema
5.1 sessionId:string
A unique session id identifying the client
5.2 server:MuRemoteRPCServer
A mock used to initiate RPCs to the remote server
5.3 client:MuClient
The underlying MuClient
object
5.4 schema:RPCProtocolSchema
The RPC protocol schema
5.5 configure(spec)
Registers event handlers specified in spec
spec:{ rpc, ready?, close? }
rpc:TableOf<ClientRPCHandler>
an object containing the implementations of the functions to be called by the serverready()
called when client is ready to handle messagesclose()
called when the client is disconnected
6 MuRemoteRPCServer
A MuRemoteRPCServer
is the client-side mock of the server
6.1 rpc:TableOf
A table of RPC initiators each under the name of the corresponding remote function on the server
credits
Copyright (c) 2017 Mikola Lysenko, Shenzhen Dianmao Technology Company Limited