Browse Source

push

master
peter 5 years ago
parent
commit
3bd83611bd
1 changed files with 31 additions and 4 deletions
  1. 31
    4
      README.md

+ 31
- 4
README.md View File

@@ -15,10 +15,10 @@ const echo = (x) => x
15 15
 const server = new Backend.RPCServer(20000, [{
16 16
     name: 'HelloWorldRPCGroup',
17 17
     exportRPCs: ()  => [
18
-        echo,
19
-        function echof(x){ return x },
18
+        echo, //named function variable
19
+        function echof(x){ return x }, //named function
20 20
         {
21
-            name: 'echoExplicit',
21
+            name: 'echoExplicit', //describing object
22 22
             call: async (x) => x
23 23
         }
24 24
     ]
@@ -31,10 +31,37 @@ client.connect().then(async () => {
31 31
     const r1 = await client['HelloWorldRPCGroup'].echof('World')
32 32
     const r2 = await client['HelloWorldRPCGroup'].echoExplicit('RPC!')
33 33
 
34
-    console.log(r0,r1,r2)
34
+    console.log(r0,r1,r2) //Hello World RPC!
35 35
 })
36 36
 
37 37
 ```
38 38
 
39
+# Using callbacks
40
+
41
+rpclibrary offers a special type of call that can be used with callbacks
42
+
43
+```typescript
44
+import {Backend, Frontend, Utils} from 'rpclibrary'
45
+
46
+const callbacks:Function[] = []
47
+const callbackserver = new Backend.RPCServer(20000, [{
48
+    name: 'HelloWorldRPCGroup',
49
+    exportRPCs: ()  => [
50
+        function  triggerCallbacks(message){ callbacks.forEach(cb => cb(message)) },
51
+        {
52
+            name: 'subscribe',
53
+            hook: async (callback) => {callbacks.push(callback); return Utils.makeSubResponse()}
54
+        }
55
+    ]
56
+}])
57
+
58
+const callbackclient = new Frontend.RPCSocket(20000, 'localhost')
59
+callbackclient.connect().then(async () => {
60
+    const r0 = await client['HelloWorldRPCGroup'].subscribe(console.log)
61
+    console.log(r0)
62
+    await client['HelloWorldRPCGroup'].triggerCallbacks("Hello!")
63
+})
64
+```
65
+
39 66
 # Documentation
40 67
 [https://gitea.frontblock.me/fw-docs/rpclibrary]

Loading…
Cancel
Save