7.示例

Syntax:

  1. --> data sent to Server
  2. <-- data sent to Client

带索引数组参数的rpc调用:

  1. --> {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}
  2. <-- {"jsonrpc": "2.0", "result": 19, "id": 1}
  3.  
  4. --> {"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2}
  5. <-- {"jsonrpc": "2.0", "result": -19, "id": 2}

带关联数组参数的rpc调用:

  1. --> {"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}
  2. <-- {"jsonrpc": "2.0", "result": 19, "id": 3}
  3.  
  4. --> {"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4}
  5. <-- {"jsonrpc": "2.0", "result": 19, "id": 4}

通知:

  1. --> {"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]}
  2. --> {"jsonrpc": "2.0", "method": "foobar"}

不包含调用方法的rpc调用:

  1. --> {"jsonrpc": "2.0", "method": "foobar", "id": "1"}
  2. <-- {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "1"}

包含无效json的rpc调用:

  1. --> {"jsonrpc": "2.0", "method": "foobar, "params": "bar", "baz]
  2. <-- {"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}

包含无效请求对象的rpc调用:

  1. --> {"jsonrpc": "2.0", "method": 1, "params": "bar"}
  2. <-- {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}

包含无效json的rpc批量调用:

  1. --> [
  2. {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
  3. {"jsonrpc": "2.0", "method"
  4. ]
  5. <-- {"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}

包含空数组的rpc调用:

  1. --> []
  2. <-- {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}

非空且无效的rpc批量调用:

  1. --> [1]
  2. <-- [
  3. {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
  4. ]

无效的rpc批量调用:

  1. --> [1,2,3]
  2. <-- [
  3. {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},
  4. {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},
  5. {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
  6. ]

rpc批量调用:

  1. --> [
  2. {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
  3. {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
  4. {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"},
  5. {"foo": "boo"},
  6. {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"},
  7. {"jsonrpc": "2.0", "method": "get_data", "id": "9"}
  8. ]
  9. <-- [
  10. {"jsonrpc": "2.0", "result": 7, "id": "1"},
  11. {"jsonrpc": "2.0", "result": 19, "id": "2"},
  12. {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},
  13. {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "5"},
  14. {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"}
  15. ]

所有都为通知的rpc批量调用:

  1. --> [
  2. {"jsonrpc": "2.0", "method": "notify_sum", "params": [1,2,4]},
  3. {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}
  4. ]
  5.  
  6. <-- //Nothing is returned for all notification batches