21个稳定版本 (7个主要版本)
18.0.0 | 2021年7月20日 |
---|---|
17.1.0 | 2021年6月7日 |
17.0.0 | 2021年1月20日 |
16.0.0 | 2020年12月14日 |
11.0.0 | 2019年4月5日 |
在 Web编程 中排名 541
每月下载量 75,150
在 103 个crate中 使用 (直接使用32个)
61KB
1.5K SLoC
Parity JSON-RPC
注意:此crate不再积极开发;如果您正在寻找积极维护的JSON RPC实现,请查看我们的 jsonrpsee crate。
Rust实现的JSON-RPC 2.0规范。支持core
和传输服务器,包括http
、ipc
、websockets
和tcp
。
新功能! 支持客户端。
子项目
- jsonrpc-core
- jsonrpc-core-client
- jsonrpc-http-server
- jsonrpc-ipc-server
- jsonrpc-tcp-server
- jsonrpc-ws-server
- jsonrpc-stdio-server
- jsonrpc-derive
- jsonrpc-server-utils
- jsonrpc-pubsub
示例
基本使用(使用HTTP传输)
use jsonrpc_http_server::jsonrpc_core::{IoHandler, Value, Params};
use jsonrpc_http_server::ServerBuilder;
fn main() {
let mut io = IoHandler::default();
io.add_method("say_hello", |_params: Params| async {
Ok(Value::String("hello".to_owned()))
});
let server = ServerBuilder::new(io)
.threads(3)
.start_http(&"127.0.0.1:3030".parse().unwrap())
.unwrap();
server.wait();
}
使用派生基本使用
use jsonrpc_core::Result;
use jsonrpc_derive::rpc;
#[rpc]
pub trait Rpc {
/// Adds two numbers and returns a result
#[rpc(name = "add")]
fn add(&self, a: u64, b: u64) -> Result<u64>;
}
pub struct RpcImpl;
impl Rpc for RpcImpl {
fn add(&self, a: u64, b: u64) -> Result<u64> {
Ok(a + b)
}
}
fn main() {
let mut io = jsonrpc_core::IoHandler::new();
io.extend_with(RpcImpl.to_delegate())
}
客户端支持
use jsonrpc_core_client::transports::local;
use jsonrpc_core::{BoxFuture, IoHandler, Result};
use jsonrpc_core::futures::{self, future, TryFutureExt};
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(future::ready(Ok("OK".to_owned())))
}
}
fn main() {
let mut io = IoHandler::new();
io.extend_with(RpcImpl.to_delegate());
let (client, server) = local::connect::<gen_client::Client, _, _>(io);
let fut = client.add(5, 6).map_ok(|res| println!("5 + 6 = {}", res));
futures::executor::block_on(async move { futures::join!(fut, server) })
.0
.unwrap();
}
lib.rs
:
JSON-RPC客户端实现的基本原语。
默认情况下,此crate不实现任何传输,使用相应的功能(tls
、http
或ws
)来选择它们。
有关更多详细信息,请参阅jsonrpc-client-transports
的文档。
依赖关系
~4–17MB
~261K SLoC