Skip to content

Commit 454cf5d

Browse files
authored
Update examples in top-level readme. (#588)
* Update examples in top-level readme. * cargo fmt --all * Added docs.
1 parent 2cb9752 commit 454cf5d

File tree

4 files changed

+82
-66
lines changed

4 files changed

+82
-66
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ Transport-agnostic `core` and transport servers for `http`, `ipc`, `websockets`
5050

5151
```rust
5252
use jsonrpc_http_server::jsonrpc_core::{IoHandler, Value, Params};
53-
use jsonrpc_http_server::{ServerBuilder};
53+
use jsonrpc_http_server::ServerBuilder;
5454

5555
fn main() {
56-
let mut io = IoHandler::new();
57-
io.add_method("say_hello", |_params: Params| {
58-
Ok(Value::String("hello".to_string()))
56+
let mut io = IoHandler::default();
57+
io.add_method("say_hello", |_params: Params| async {
58+
Ok(Value::String("hello".to_owned()))
5959
});
6060

6161
let server = ServerBuilder::new(io)
@@ -97,7 +97,6 @@ fn main() {
9797

9898
```rust
9999
use jsonrpc_core_client::transports::local;
100-
use jsonrpc_core::futures::future::{self, Future, FutureResult};
101100
use jsonrpc_core::{Error, IoHandler, Result};
102101
use jsonrpc_derive::rpc;
103102

@@ -143,5 +142,4 @@ fn main() {
143142
};
144143
fut.wait().unwrap();
145144
}
146-
147145
```

core/README.md

Lines changed: 0 additions & 59 deletions
This file was deleted.

derive/examples/client-local.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use jsonrpc_core::{
2+
futures::{self, FutureExt},
3+
BoxFuture, IoHandler, Result,
4+
};
5+
use jsonrpc_core_client::transports::local;
6+
use jsonrpc_derive::rpc;
7+
8+
/// Rpc trait
9+
#[rpc]
10+
pub trait Rpc {
11+
/// Returns a protocol version
12+
#[rpc(name = "protocolVersion")]
13+
fn protocol_version(&self) -> Result<String>;
14+
15+
/// Adds two numbers and returns a result
16+
#[rpc(name = "add", alias("callAsyncMetaAlias"))]
17+
fn add(&self, a: u64, b: u64) -> Result<u64>;
18+
19+
/// Performs asynchronous operation
20+
#[rpc(name = "callAsync")]
21+
fn call(&self, a: u64) -> BoxFuture<Result<String>>;
22+
}
23+
24+
struct RpcImpl;
25+
26+
impl Rpc for RpcImpl {
27+
fn protocol_version(&self) -> Result<String> {
28+
Ok("version1".into())
29+
}
30+
31+
fn add(&self, a: u64, b: u64) -> Result<u64> {
32+
Ok(a + b)
33+
}
34+
35+
fn call(&self, _: u64) -> BoxFuture<Result<String>> {
36+
Box::pin(futures::future::ready(Ok("OK".to_owned())))
37+
}
38+
}
39+
40+
fn main() {
41+
futures::executor::block_on(async {
42+
let mut io = IoHandler::new();
43+
io.extend_with(RpcImpl.to_delegate());
44+
println!("Starting local server");
45+
let (client, server) = local::connect(io);
46+
let client = use_client(client).fuse();
47+
let server = server.fuse();
48+
49+
futures::pin_mut!(client);
50+
futures::pin_mut!(server);
51+
52+
futures::select! {
53+
server = server => {},
54+
client = client => {},
55+
}
56+
});
57+
}
58+
59+
async fn use_client(client: RpcClient) {
60+
let res = client.add(5, 6).await.unwrap();
61+
println!("5 + 6 = {}", res);
62+
}

derive/src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,22 @@
120120
//! }
121121
//! }
122122
//!
123-
//! # fn main() {}
123+
//! fn main() {
124+
//! let mut io = jsonrpc_core::MetaIoHandler::default();
125+
//! io.extend_with(RpcImpl::default().to_delegate());
126+
//!
127+
//! let server_builder = jsonrpc_tcp_server::ServerBuilder::with_meta_extractor(
128+
//! io,
129+
//! |request: &jsonrpc_tcp_server::RequestContext| Arc::new(Session::new(request.sender.clone()))
130+
//! );
131+
//! let server = server_builder
132+
//! .start(&"127.0.0.1:3030".parse().unwrap())
133+
//! .expect("Unable to start TCP server");
134+
//!
135+
//! // The server spawns a separate thread. Dropping the `server` handle causes it to close.
136+
//! // Uncomment the line below to keep the server running in your example.
137+
//! // server.wait();
138+
//! }
124139
//! ```
125140
//!
126141
//! Client Example

0 commit comments

Comments
 (0)