Browse Source

push

master
peter 4 years ago
parent
commit
530f55bc90
1 changed files with 23 additions and 9 deletions
  1. 23
    9
      README.md

+ 23
- 9
README.md View File

@@ -43,23 +43,37 @@ rpclibrary offers a special type of call that can be used with callbacks
43 43
 ```typescript
44 44
 import {Backend, Frontend, Utils} from 'rpclibrary'
45 45
 
46
-const callbacks:Function[] = []
47
-const callbackserver = new Backend.RPCServer(20000, [{
46
+const callbacks:Map<string, Function> = new Map()
47
+
48
+const server = new Backend.RPCServer(20000, [{
48 49
     name: 'HelloWorldRPCGroup',
49 50
     exportRPCs: ()  => [
50
-        function  triggerCallbacks(message){ callbacks.forEach(cb => cb(message)) },
51
+        function  triggerCallbacks(...messages){ callbacks.forEach(cb => cb.apply({}, messages)) },
51 52
         {
52 53
             name: 'subscribe',
53
-            hook: async (callback) => {callbacks.push(callback); return Utils.makeSubResponse()}
54
+            hook: async (callback) => {
55
+                const resp = Utils.makeSubResponse()
56
+                callbacks.set(resp.uuid, callback); 
57
+                return resp
58
+            }
59
+        },{
60
+            name: 'unsubscribe',
61
+            call: async (uuid) => { callbacks.delete(uuid) }
54 62
         }
55 63
     ]
56 64
 }])
57 65
 
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!")
66
+const client = new Frontend.RPCSocket(20000, 'localhost')
67
+client.connect().then(async () => {
68
+    const res = await client['HelloWorldRPCGroup'].subscribe(async (...args:any) => {
69
+        console.log.apply(console, args)
70
+
71
+        /* close the callbacks once you're done */
72
+        await client['HelloWorldRPCGroup'].unsubscribe(res.uuid)
73
+        client.unhook(res.uuid)
74
+    })
75
+
76
+    await client['HelloWorldRPCGroup'].triggerCallbacks("Hello", "World", "Callbacks")
63 77
 })
64 78
 ```
65 79
 

Loading…
Cancel
Save