From 4d51e3211740764764a6423f8ead4944e1790341 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Sun, 19 Nov 2023 22:19:06 +0300 Subject: karyons jsonrpc implementation --- jsonrpc/README.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 jsonrpc/README.md (limited to 'jsonrpc/README.md') diff --git a/jsonrpc/README.md b/jsonrpc/README.md new file mode 100644 index 0000000..82ab2e9 --- /dev/null +++ b/jsonrpc/README.md @@ -0,0 +1,50 @@ +# karyons jsonrpc + +A fast and lightweight async [JSONRPC2.0](https://www.jsonrpc.org/specification) implementation. + +## Example + +```rust +use std::sync::Arc; + +use serde_json::Value; + +use karyons_jsonrpc::{JsonRPCError, Server, Client, register_service}; + +struct HelloWorld {} + +impl HelloWorld { + async fn say_hello(&self, params: Value) -> Result { + let msg: String = serde_json::from_value(params)?; + Ok(serde_json::json!(format!("Hello {msg}!"))) + } +} + +////////////////// +// Server +////////////////// +let ex = Arc::new(smol::Executor::new()); + +// Creates a new server +let endpoint = "tcp://127.0.0.1:60000".parse().unwrap(); +let server = Server::new_with_endpoint(&endpoint, ex.clone()).await.unwrap(); + +// Register the HelloWorld service +register_service!(HelloWorld, say_hello); +server.attach_service(HelloWorld{}); + +// Starts the server +ex.run(server.start()); + + +////////////////// +// Client +////////////////// + +// Creates a new client +let endpoint = "tcp://127.0.0.1:60000".parse().unwrap(); +let client = Client::new_with_endpoint(&endpoint, None).await.unwrap(); + +let result: String = client.call("HelloWorld.say_hello", "world".to_string()).await.unwrap(); + +``` -- cgit v1.2.3