#json-rpc #rpc #json-rpc-server #json #serde #tcp-server

tetsy-jsonrpc-server-utils

Tetsy 服务器工具,用于 tetsy-jsonrpc-core 库

显示库…

3 个稳定版本

15.1.0 2021 年 3 月 13 日
14.2.1 2021 年 3 月 1 日

#64#json-rpc-server

Download history 53/week @ 2024-03-11 59/week @ 2024-03-18 102/week @ 2024-03-25 139/week @ 2024-04-01 49/week @ 2024-04-08 51/week @ 2024-04-15 52/week @ 2024-04-22 49/week @ 2024-04-29 56/week @ 2024-05-06 65/week @ 2024-05-13 39/week @ 2024-05-20 60/week @ 2024-05-27 46/week @ 2024-06-03 48/week @ 2024-06-10 46/week @ 2024-06-17 64/week @ 2024-06-24

206 每月下载量
36 个库中使用 (6 个直接使用)

MIT 许可证

105KB
3K SLoC

Parity JSON-RPC

Rust 实现 JSON-RPC 2.0 规范。支持 core 和传输服务器,如 httpipcwebsocketstcp

新功能! 支持客户端。

文档

子项目

示例

基本用法(使用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, u64, 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::{Error, IoHandler, Result};
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) -> FutureResult<String, Error>;
}

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) -> FutureResult<String, Error> {
		future::ok("OK".to_owned())
	}
}

fn main() {
	let mut io = IoHandler::new();
	io.extend_with(RpcImpl.to_delegate());

	let fut = {
		let (client, server) = local::connect::<gen_client::Client, _, _>(io);
		client.add(5, 6).map(|res| println!("5 + 6 = {}", res)).join(server)
	};
	fut.wait().unwrap();
}

依赖

~9.5MB
~162K SLoC