Skip to content

Commit

Permalink
Update examples in top-level readme. (#588)
Browse files Browse the repository at this point in the history
* Update examples in top-level readme.

* cargo fmt --all

* Added docs.
  • Loading branch information
tomusdrw authored Nov 6, 2020
1 parent 2cb9752 commit 454cf5d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 66 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ Transport-agnostic `core` and transport servers for `http`, `ipc`, `websockets`

```rust
use jsonrpc_http_server::jsonrpc_core::{IoHandler, Value, Params};
use jsonrpc_http_server::{ServerBuilder};
use jsonrpc_http_server::ServerBuilder;

fn main() {
let mut io = IoHandler::new();
io.add_method("say_hello", |_params: Params| {
Ok(Value::String("hello".to_string()))
let mut io = IoHandler::default();
io.add_method("say_hello", |_params: Params| async {
Ok(Value::String("hello".to_owned()))
});

let server = ServerBuilder::new(io)
Expand Down Expand Up @@ -97,7 +97,6 @@ fn main() {

```rust
use jsonrpc_core_client::transports::local;
use jsonrpc_core::futures::future::{self, Future, FutureResult};
use jsonrpc_core::{Error, IoHandler, Result};
use jsonrpc_derive::rpc;

Expand Down Expand Up @@ -143,5 +142,4 @@ fn main() {
};
fut.wait().unwrap();
}

```
59 changes: 0 additions & 59 deletions core/README.md

This file was deleted.

62 changes: 62 additions & 0 deletions derive/examples/client-local.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use jsonrpc_core::{
futures::{self, FutureExt},
BoxFuture, IoHandler, Result,
};
use jsonrpc_core_client::transports::local;
use jsonrpc_derive::rpc;

/// Rpc trait
#[rpc]
pub trait Rpc {
/// Returns a protocol version
#[rpc(name = "protocolVersion")]
fn protocol_version(&self) -> Result<String>;

/// Adds two numbers and returns a result
#[rpc(name = "add", alias("callAsyncMetaAlias"))]
fn add(&self, a: u64, b: u64) -> Result<u64>;

/// Performs asynchronous operation
#[rpc(name = "callAsync")]
fn call(&self, a: u64) -> BoxFuture<Result<String>>;
}

struct RpcImpl;

impl Rpc for RpcImpl {
fn protocol_version(&self) -> Result<String> {
Ok("version1".into())
}

fn add(&self, a: u64, b: u64) -> Result<u64> {
Ok(a + b)
}

fn call(&self, _: u64) -> BoxFuture<Result<String>> {
Box::pin(futures::future::ready(Ok("OK".to_owned())))
}
}

fn main() {
futures::executor::block_on(async {
let mut io = IoHandler::new();
io.extend_with(RpcImpl.to_delegate());
println!("Starting local server");
let (client, server) = local::connect(io);
let client = use_client(client).fuse();
let server = server.fuse();

futures::pin_mut!(client);
futures::pin_mut!(server);

futures::select! {
server = server => {},
client = client => {},
}
});
}

async fn use_client(client: RpcClient) {
let res = client.add(5, 6).await.unwrap();
println!("5 + 6 = {}", res);
}
17 changes: 16 additions & 1 deletion derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,22 @@
//! }
//! }
//!
//! # fn main() {}
//! fn main() {
//! let mut io = jsonrpc_core::MetaIoHandler::default();
//! io.extend_with(RpcImpl::default().to_delegate());
//!
//! let server_builder = jsonrpc_tcp_server::ServerBuilder::with_meta_extractor(
//! io,
//! |request: &jsonrpc_tcp_server::RequestContext| Arc::new(Session::new(request.sender.clone()))
//! );
//! let server = server_builder
//! .start(&"127.0.0.1:3030".parse().unwrap())
//! .expect("Unable to start TCP server");
//!
//! // The server spawns a separate thread. Dropping the `server` handle causes it to close.
//! // Uncomment the line below to keep the server running in your example.
//! // server.wait();
//! }
//! ```
//!
//! Client Example
Expand Down

0 comments on commit 454cf5d

Please sign in to comment.