This commit is contained in:
peter
2019-09-22 17:14:32 +02:00
parent ed3da909c6
commit 3bd83611bd
+31 -4
View File
@@ -15,10 +15,10 @@ const echo = (x) => x
const server = new Backend.RPCServer(20000, [{ const server = new Backend.RPCServer(20000, [{
name: 'HelloWorldRPCGroup', name: 'HelloWorldRPCGroup',
exportRPCs: () => [ exportRPCs: () => [
echo, echo, //named function variable
function echof(x){ return x }, function echof(x){ return x }, //named function
{ {
name: 'echoExplicit', name: 'echoExplicit', //describing object
call: async (x) => x call: async (x) => x
} }
] ]
@@ -31,10 +31,37 @@ client.connect().then(async () => {
const r1 = await client['HelloWorldRPCGroup'].echof('World') const r1 = await client['HelloWorldRPCGroup'].echof('World')
const r2 = await client['HelloWorldRPCGroup'].echoExplicit('RPC!') const r2 = await client['HelloWorldRPCGroup'].echoExplicit('RPC!')
console.log(r0,r1,r2) console.log(r0,r1,r2) //Hello World RPC!
}) })
``` ```
# Using callbacks
rpclibrary offers a special type of call that can be used with callbacks
```typescript
import {Backend, Frontend, Utils} from 'rpclibrary'
const callbacks:Function[] = []
const callbackserver = new Backend.RPCServer(20000, [{
name: 'HelloWorldRPCGroup',
exportRPCs: () => [
function triggerCallbacks(message){ callbacks.forEach(cb => cb(message)) },
{
name: 'subscribe',
hook: async (callback) => {callbacks.push(callback); return Utils.makeSubResponse()}
}
]
}])
const callbackclient = new Frontend.RPCSocket(20000, 'localhost')
callbackclient.connect().then(async () => {
const r0 = await client['HelloWorldRPCGroup'].subscribe(console.log)
console.log(r0)
await client['HelloWorldRPCGroup'].triggerCallbacks("Hello!")
})
```
# Documentation # Documentation
[https://gitea.frontblock.me/fw-docs/rpclibrary] [https://gitea.frontblock.me/fw-docs/rpclibrary]