Skip to content

Commit

Permalink
Fix example in the README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Jan 7, 2024
1 parent 09d1281 commit c688889
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ When an error occurs, the `error` is populated, otherwise `result` will be popul
Here's a simple example:

```nim
import json_rpc/rpcserver
import
json_rpc/rpcserver
var router = newRpcRouter()
var router = RpcRouter.init()
router.rpc("hello") do() -> string:
result = "Hello"
router.rpc("hello") do():
result = %"Hello"
```

As no return type was specified in this example, `result` defaults to the `JsonNode` type.
Expand All @@ -69,10 +70,14 @@ The return type then performs the opposite process, converting Nim types to Json
Here is a more complex parameter example:

```nim
import
json_rpc/rpcserver,
json_rpc/jsonmarshal
type
HeaderKind = enum hkOne, hkTwo, hkThree
Header = ref object
Header = object
kind: HeaderKind
size: int64
Expand All @@ -84,9 +89,15 @@ type
data: DataBlob
name: string
Header.useDefaultSerializationIn JrpcConv
DataBlob.useDefaultSerializationIn JrpcConv
MyObject.useDefaultSerializationIn JrpcConv
router.rpc("updateData") do(myObj: MyObject, newData: DataBlob) -> DataBlob:
result = myObj.data
myObj.data = newData
if myObj.name == "old":
result = myObj.data
else:
result = newData
```

Behind the scenes, all RPC calls take parameters through `RequestParamsRx` structure.
Expand Down Expand Up @@ -256,7 +267,7 @@ Here's an example of how that looks by manually creating the JSON. Later we will
```nim
let call = %*{
"id": %1,
"jsonrpc": %2.0,
"jsonrpc": %"2.0",
"method": %"hello",
"params": %["Terry"]
}
Expand All @@ -265,7 +276,7 @@ let localResult = waitFor router.route(call)
echo localResult
# We should see something like this
# {"jsonrpc":"2.0","id":1,"result":"Hello Terry","error":null}
# {"jsonrpc":"2.0","id":1,"result":"Hello Terry"}
```

# Server
Expand Down Expand Up @@ -332,8 +343,8 @@ waitFor client.connect("localhost", Port(8545))
let response = waitFor client.call("hello", %[%"Daisy"])
# the call returns a `Response` type which contains the result
echo response.result
# the call returns a `JsonString` type which contains the result
echo response
```

### `createRpcSigs`
Expand Down

0 comments on commit c688889

Please sign in to comment.