|
3 | 3 | Rust implementation of JSON-RPC 2.0 Specification. |
4 | 4 | Transport-agnostic `core` and transport servers for `http`, `ipc`, `websockets` and `tcp`. |
5 | 5 |
|
| 6 | +**New!** Support for [clients](#Client-support). |
| 7 | + |
6 | 8 | [![Build Status][travis-image]][travis-url] |
7 | 9 | [![Build Status][appveyor-image]][appveyor-url] |
8 | 10 |
|
@@ -94,3 +96,57 @@ fn main() { |
94 | 96 | let mut io = jsonrpc_core::IoHandler::new(); |
95 | 97 | io.extend_with(RpcImpl.to_delegate()) |
96 | 98 | } |
| 99 | +``` |
| 100 | + |
| 101 | +### Client support |
| 102 | + |
| 103 | +```rust |
| 104 | +use jsonrpc_client::local; |
| 105 | +use jsonrpc_core::futures::future::{self, Future, FutureResult}; |
| 106 | +use jsonrpc_core::{Error, IoHandler, Result}; |
| 107 | +use jsonrpc_derive::rpc; |
| 108 | + |
| 109 | +/// Rpc trait |
| 110 | +#[rpc] |
| 111 | +pub trait Rpc { |
| 112 | + /// Returns a protocol version |
| 113 | + #[rpc(name = "protocolVersion")] |
| 114 | + fn protocol_version(&self) -> Result<String>; |
| 115 | + |
| 116 | + /// Adds two numbers and returns a result |
| 117 | + #[rpc(name = "add", alias("callAsyncMetaAlias"))] |
| 118 | + fn add(&self, a: u64, b: u64) -> Result<u64>; |
| 119 | + |
| 120 | + /// Performs asynchronous operation |
| 121 | + #[rpc(name = "callAsync")] |
| 122 | + fn call(&self, a: u64) -> FutureResult<String, Error>; |
| 123 | +} |
| 124 | + |
| 125 | +struct RpcImpl; |
| 126 | + |
| 127 | +impl Rpc for RpcImpl { |
| 128 | + fn protocol_version(&self) -> Result<String> { |
| 129 | + Ok("version1".into()) |
| 130 | + } |
| 131 | + |
| 132 | + fn add(&self, a: u64, b: u64) -> Result<u64> { |
| 133 | + Ok(a + b) |
| 134 | + } |
| 135 | + |
| 136 | + fn call(&self, _: u64) -> FutureResult<String, Error> { |
| 137 | + future::ok("OK".to_owned()) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +fn main() { |
| 142 | + let mut io = IoHandler::new(); |
| 143 | + io.extend_with(RpcImpl.to_delegate()); |
| 144 | + |
| 145 | + let fut = { |
| 146 | + let (client, server) = local::connect::<gen_client::Client, _, _>(io); |
| 147 | + client.add(5, 6).map(|res| println!("5 + 6 = {}", res)).join(server) |
| 148 | + }; |
| 149 | + fut.wait().unwrap(); |
| 150 | +} |
| 151 | + |
| 152 | +``` |
0 commit comments