Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/harlanc/jsonrpc2-rs
A JSON-RPC 2.0 client/server library in rust.
https://github.com/harlanc/jsonrpc2-rs
json-rpc json-rpc-client json-rpc-server json-rpc2 rpc rust websocket
Last synced: 3 months ago
JSON representation
A JSON-RPC 2.0 client/server library in rust.
- Host: GitHub
- URL: https://github.com/harlanc/jsonrpc2-rs
- Owner: harlanc
- License: mit
- Created: 2022-11-08T05:32:59.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-03T00:32:14.000Z (almost 2 years ago)
- Last Synced: 2024-09-15T04:54:12.764Z (4 months ago)
- Topics: json-rpc, json-rpc-client, json-rpc-server, json-rpc2, rpc, rust, websocket
- Language: Rust
- Homepage:
- Size: 82 KB
- Stars: 10
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jsonrpc2-rs
[![crates.io](https://img.shields.io/crates/v/jrpc2.svg)](https://crates.io/crates/jrpc2)
A JSON-RPC 2.0 client/server library in rust.
# How to use
## Define the structed value types
You should define the types of the follwing three structed values:
- Request Parameters [Reference](https://www.jsonrpc.org/specification#parameter_structures)
- Response Result [Reference](https://www.jsonrpc.org/specification#response_object)
- Error Data [Reference](https://www.jsonrpc.org/specification#error_object)For example:
type RequestParams = Vec;
type ResponseResult = u32;
type ErrorData = String;
## Define the handlerYou should implement the THandler trait to define the server side logic:
#[async_trait]
pub trait THandler
where
S: Serialize,
{
async fn handle(&self, conn: Arc>, request: Request);
}
For example:
struct Add {}#[async_trait]
impl THandler for Add {
async fn handle(
&self,
json_rpc2: Arc>,
request: Request,
) {
match request.method.as_str() {
"add" => {
let params = request.params.unwrap();
let add_res: u32 = params.iter().sum();
let response = Response::new(request.id.unwrap(), Some(add_res), None);
json_rpc2.response(response).unwrap();
}_ => {
log::info!("unknow method");
}
}
}
}## Init JSON-RPC server
let addr = "127.0.0.1:9002";
let listener = TcpListener::bind(&addr).await.expect("Can't listen");if let Ok((stream, _)) = listener.accept().await {
let server_stream = ServerObjectStream::accept(stream)
.await
.expect("cannot generate object stream");
JsonRpc2::new(Box::new(server_stream), Some(Box::new(Add {}))).await;
}
## Init JSON-RPC clientlet url = url::Url::parse("ws://127.0.0.1:9002/").unwrap();
let client_stream = ClientObjectStream::connect(url)
.await
.expect("cannot generate object stream");let conn_arc =
JsonRpc2::<_, ResponseResult, ErrorData>::new(Box::new(client_stream), None).await;
## Call the functionmatch conn_arc.call("add", Some(vec![2u32, 3u32, 4u32])).await {
Ok(response) => {
let result = response.result.unwrap();
assert_eq!(result, 9);
}
Err(err) => {
log::error!("call add error: {}", err);
}
}