ts-rpc
- npm i ts-rpc-node
- source code
- See bin/test for javascript example
How it works?
Your all services connnect to a proxy server. The proxy server is responsible for forwarding data packets. If the proxy server is not connected, the client will continue to try again and again.
typescript example
// server.ts// Create proxy server.; ; ;
// client1.ts; ; test;
// client2.ts;; ; test;
simplerpc()
Wrapper of RPC function
Possible signatures:
simplerpc(rpcFunc)
simplerpc(options,rpcFunc)
simplerpc(rpcFunc)
Parameters:
rpcFunc
«Function»
Returns:
- «Function»
simplerpc(options,rpcFunc)
Parameters:
options
«ICallFuncOption»rpcFunc
«Function»
Returns:
- «Function»
promirpc()
Wrapper of RPC function, return by promise.
Possible signatures:
promirpc(rpcFunc)
promirpc(options,rpcFunc)
promirpc(rpcFunc)
Parameters:
rpcFunc
«Function»
Returns:
- «Promise<T>» T is rpcFunc's return type.
promirpc(rpcFunc)
Parameters:
options
«ICallFuncOption»rpcFunc
«Function»
Returns:
- «Promise<T>» T is rpcFunc's return type.
ClientHandle
Class:Event:CLIENTHANDLE_EVENT.REGISTED
key
«string» My service's name.others
«string[]»Name Array of other services.
Emitted when the client successfully registe in proxy server.
Event:CLIENTHANDLE_EVENT.CLOSE
error
«any»
Emitted when the connection between client and proxy server is disconnected.
Event:CLIENTHANDLE_EVENT.OTHER_RIGISTED
key
«string» Other service's name.
Emitted when other service successfully registe in proxy server.
Event:CLIENTHANDLE_EVENT.OTHER_CLOSE
key
«string» Other service's name.
Emitted when the connection between a service and proxy server is disconnected.
client.SetEnv
app
«any» the context
Set the context for other app's rpc
client.Remove
Disconnect from proxy server and release all references.
client.ProxyRoute
[attr]
«string»
Proxy attribute.
// client1.tsapp.rpc=client.ProxyRoute'recv' as TestA;simplerpc,app.rpc.print'everybody';
// client2.tsapp.recv=new TestA;
client.SetCallFuncOpts
opts
«ICallFuncOption|undefined»
simplerpc or promirpc will use this opts if you do not give a ICallFuncOption. client.SetCallFuncOpts(undefined) will clear opts you set before.
client.setEncodeDecode
encode
«Function»decode
«Function»
The default encode:JSON.stringify,decode:JSON.parse. Call this method to set your own encode,decode for rpc's arguments.
createClient
Parameters:
opts
«IClientOptions»[app]
«any»[cb]
«Function»
Returns:
createClientSync
Parameters:
opts
«IClientOptions»[app]
«any»
Returns:
- «Promise<ClientHandle>»
ServerHandle
Class:Properties:
tokencheck
«boolean»Open tokencheck by setting true, close by false. True is Default.If it's false, checking will not effect either client sends token or not.newtoken
«string»You can Update token string. It will be priority before IServerOptions.token.
server.Remove
Close proxy server and release all references.
createProxy
Parameters:
opts
«IServerOptions»
Returns:
interface
ICallFuncOption
service
«string|string[]» The target service's name.This name is the unique identifier for each service.Giving a string array means the rpc will be broadcast to these services as a notice.[timeout]
«number»[ignorecb]
«boolean» Default false. When you use simplerpc, if the last argument is Function type, it means a notice, not a request. Set the option true to force the rpc to be a notice.And The last argument will be encode even it is a Function.[bytes]
«number» The default size of a pack is 1MB. And the head of pack may take tens of bytes.You can set a right number by this option.
IClientOptions
service
«string» The unique string different from ench service.connect
«Object»host
«string»port
«number»
[logger]
«any» You can set console or log4js to print logs.[coding]
«string»[callback_timeout]
«number»[token]
«string»
IServerOptions
listen
«Object»host
«string»port
«number»[exclusive]
«boolean»
[logger]
«any» You can set console or log4js to print logs.[coding]
«string»[token]
«string»