7. 示例
最后更新于:2022-04-01 01:20:12
**语法:**
> --> data sent to Server
> data sent to Client
带索引数组参数的rpc调用:
~~~
--> {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}
{"jsonrpc": "2.0", "result": 19, "id": 1}
--> {"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2}
{"jsonrpc": "2.0", "result": -19, "id": 2}
~~~
带关联数组参数的rpc调用:
~~~
--> {"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}
{"jsonrpc": "2.0", "result": 19, "id": 3}
--> {"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4}
{"jsonrpc": "2.0", "result": 19, "id": 4}
~~~
通知:
~~~
--> {"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]}
--> {"jsonrpc": "2.0", "method": "foobar"}
~~~
不包含调用方法的rpc调用:
~~~
--> {"jsonrpc": "2.0", "method": "foobar", "id": "1"}
{"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "1"}
~~~
包含无效json的rpc调用:
~~~
--> {"jsonrpc": "2.0", "method": "foobar, "params": "bar", "baz]
{"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}
~~~
包含无效请求对象的rpc调用:
~~~
--> {"jsonrpc": "2.0", "method": 1, "params": "bar"}
{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
~~~
包含无效json的rpc批量调用:
~~~
--> [
{"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
{"jsonrpc": "2.0", "method"
]
{"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}
~~~
包含空数组的rpc调用:
~~~
--> []
{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
~~~
非空且无效的rpc批量调用:
~~~
--> [1]
[
{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
]
~~~
无效的rpc批量调用:
~~~
--> [1,2,3]
[
{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},
{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},
{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
]
~~~
rpc批量调用:
~~~
--> [
{"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
{"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
{"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"},
{"foo": "boo"},
{"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"},
{"jsonrpc": "2.0", "method": "get_data", "id": "9"}
]
[
{"jsonrpc": "2.0", "result": 7, "id": "1"},
{"jsonrpc": "2.0", "result": 19, "id": "2"},
{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},
{"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "5"},
{"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"}
]
~~~
所有都为通知的rpc批量调用:
~~~
--> [
{"jsonrpc": "2.0", "method": "notify_sum", "params": [1,2,4]},
{"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}
]
//Nothing is returned for all notification batches
~~~