Crates.io | jsonlrpc |
lib.rs | jsonlrpc |
version | |
source | src |
created_at | 2024-09-05 14:08:35.459397 |
updated_at | 2024-12-02 12:49:10.456785 |
description | A JSON-RPC 2.0 library that streams JSON objects in JSON Lines format |
homepage | https://github.com/sile/jsonlrpc |
repository | https://github.com/sile/jsonlrpc |
max_upload_size | |
id | 1364686 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
A JSON-RPC 2.0 library that streams JSON objects in JSON Lines format.
RPC client:
use std::net::TcpStream;
use jsonlrpc::{RpcClient, RequestId, RequestObject, ResponseObject, JsonRpcVersion};
// Connect to a JSON-RPC server.
let server_addr = spawn_rpc_server_thread(); // See below
let socket = TcpStream::connect(server_addr).expect("failed to connect to server");
let mut client = RpcClient::new(socket);
// Send a request to the server.
let request = RequestObject {
jsonrpc: JsonRpcVersion::V2,
id: Some(RequestId::Number(1)),
method: "foo".to_string(),
params: None,
};
let response = client.call(&request).expect("failed to RPC call");
// Check the response.
let Some(ResponseObject::Ok { result, id, .. }) = response else {
panic!("expected ok response, got notification or err response")
};
assert_eq!(id, RequestId::Number(1));
RPC server:
use std::net::{SocketAddr, TcpListener};
use jsonlrpc::{JsonlStream, JsonRpcVersion, RequestObject, ResponseObject};
fn spawn_server_thread() -> SocketAddr {
let listener = TcpListener::bind("127.0.0.1:0").expect("failed to bind to address");
let addr = listener.local_addr().expect("failed to get local address");
std::thread::spawn(move || {
for stream in listener.incoming() {
let stream = stream.expect("failed to accept incoming connection");
let mut stream = JsonlStream::new(stream);
std::thread::spawn(move || {
let request: RequestObject = stream.read_value().expect("failed to read request");
let response = ResponseObject::Ok {
jsonrpc: JsonRpcVersion::V2,
id: request.id.expect("expected request id"),
result: serde_json::Value::String(request.method),
};
stream.write_value(&response).expect("failed to write response");
});
}
});
addr
}