fix examples

This commit is contained in:
Daniel Huebleitner
2019-10-29 19:42:44 +01:00
parent f09667cd57
commit bbfc197d75
2 changed files with 17 additions and 11 deletions
+16 -10
View File
@@ -8,11 +8,14 @@ npm i rpclibrary
# Quickstart # Quickstart
```typescript ```typescript
import {Backend, Frontend} from 'rpclibrary' import {RPCServer, RPCSocket} from 'rpclibrary'
const port = 1234
const host = 'locahost'
const echo = (x) => x const echo = (x) => x
const server = new Backend.RPCServer(port, [{ const server = new RPCServer(port, [{
name: 'HelloWorldRPCGroup', name: 'HelloWorldRPCGroup',
exportRPCs: () => [ exportRPCs: () => [
echo, //named function variable echo, //named function variable
@@ -24,7 +27,7 @@ const server = new Backend.RPCServer(port, [{
] ]
}]) }])
const client = new Frontend.RPCSocket(port, host) const client = new RPCSocket(port, host)
client.connect().then(async () => { client.connect().then(async () => {
const r0 = await client['HelloWorldRPCGroup'].echo('Hello') const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
@@ -47,11 +50,14 @@ The uuid, as the name implies, is used to uniquely identify the callback for a g
You should unhook the client socket once you're done with it as not to cause security or control flow issues. You should unhook the client socket once you're done with it as not to cause security or control flow issues.
```typescript ```typescript
import {Backend, Frontend, Utils} from 'rpclibrary' import {RPCServer, RPCSocket} from 'rpclibrary'
const port = 1234
const host = 'locahost'
const callbacks:Map<string, Function> = new Map() const callbacks:Map<string, Function> = new Map()
const server = new Backend.RPCServer(port, [{ new RPCServer(port, [{
name: 'HelloWorldRPCGroup', name: 'HelloWorldRPCGroup',
exportRPCs: () => [ exportRPCs: () => [
function triggerCallbacks(...messages){ callbacks.forEach(cb => cb.apply({}, messages)) }, function triggerCallbacks(...messages){ callbacks.forEach(cb => cb.apply({}, messages)) },
@@ -69,7 +75,7 @@ const server = new Backend.RPCServer(port, [{
] ]
}]) }])
const client = new Frontend.RPCSocket(port, host) const client = new RPCSocket(port, host)
client.connect().then(async () => { client.connect().then(async () => {
const res = await client['HelloWorldRPCGroup'].subscribe(async (...args:any) => { const res = await client['HelloWorldRPCGroup'].subscribe(async (...args:any) => {
console.log.apply(console, args) console.log.apply(console, args)
@@ -86,7 +92,7 @@ client.connect().then(async () => {
If you need to include further response data into your `SubscriptionResponse` you can extend it using the server's first generic parameter `SubResType` If you need to include further response data into your `SubscriptionResponse` you can extend it using the server's first generic parameter `SubResType`
```typescript ```typescript
new RPCServer<{extension: string}>(port, { new RPCServer<{extension: string}>(port, [{
name: 'MyRPCGroup', name: 'MyRPCGroup',
exportRPCs: () => [{ exportRPCs: () => [{
name: 'subscribe', name: 'subscribe',
@@ -97,8 +103,8 @@ new RPCServer<{extension: string}>(port, {
extension: 'your_data_here' //tsc will demand this field extension: 'your_data_here' //tsc will demand this field
} }
} }
}] }]}
}) ])
``` ```
@@ -126,7 +132,7 @@ RPCSocket.makeSocket<MyInterface>(port, host).then((async (client) => {
/* OR */ /* OR */
const client = new Frontend.RPCSocket(port, host) const client = new RPCSocket(port, host)
client.connect<MyInterface>().then((async (client) => { client.connect<MyInterface>().then((async (client) => {
const r = await client.Group2.echo("hee") //tsc knows about available RPCs const r = await client.Group2.echo("hee") //tsc knows about available RPCs
})) }))
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "rpclibrary", "name": "rpclibrary",
"version": "1.3.8", "version": "1.3.9",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {