A Rust implementation of MessagePack-RPC inspired by
msgpack-rpc-rust
, but based on
tokio.
Here is a minimalistic example showing how to write a simple server and client. For a more advanced example you can take a look at the calculator example.
```rust extern crate futures; extern crate tokiocore; extern crate tokioproto; extern crate rmp_rpc;
use tokioproto::TcpServer; use rmprpc::{Server, Protocol, Dispatch, Client}; use rmprpc::msgpack::{Value, Utf8String}; use tokiocore::reactor::Core; use std::thread; use std::time::Duration; use futures::Future;
pub struct HelloWorld;
// a server with two methods, "hello" that returns "hello", and "world" that return "world"
impl Dispatch for HelloWorld {
fn dispatch(&mut self, method: &str, _params: &[Value]) -> Result
fn main() { let addr = "127.0.0.1:54321".parse().unwrap();
// start the server in the background
thread::spawn(move || {
let tcp_server = TcpServer::new(Protocol, addr);
tcp_server.serve(|| {
Ok(Server::new(HelloWorld))
});
});
thread::sleep(Duration::from_millis(100));
// client part
let mut core = Core::new().unwrap();
let handle = core.handle();
core.run(
Client::connect(&addr, &handle)
.and_then(|client| {
client.request("hello", vec![])
.and_then(move |response| {
println!("client: {:?}", response);
client.request("world", vec![])
})
.and_then(|response| {
println!("client: {:?}", response);
Ok(())
})
})
).unwrap();
} ```