aboutsummaryrefslogtreecommitdiff
path: root/jsonrpc/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'jsonrpc/README.md')
-rw-r--r--jsonrpc/README.md73
1 files changed, 47 insertions, 26 deletions
diff --git a/jsonrpc/README.md b/jsonrpc/README.md
index af7dfe2..98c18e1 100644
--- a/jsonrpc/README.md
+++ b/jsonrpc/README.md
@@ -1,52 +1,73 @@
# karyon jsonrpc
A fast and lightweight async implementation of [JSON-RPC
-2.0](https://www.jsonrpc.org/specification), supporting the Tcp and Unix protocols.
+2.0](https://www.jsonrpc.org/specification).
+
+features:
+- Supports TCP, TLS, WebSocket, and Unix protocols.
+- Uses smol(async-std) as the async runtime, but also supports tokio via the
+ `tokio` feature.
+- Allows registration of multiple services (structs) of different types on a
+ single server.
## Example
-```rust
+```
use std::sync::Arc;
use serde_json::Value;
use smol::net::{TcpStream, TcpListener};
-use karyon_jsonrpc::{JsonRPCError, Server, Client, register_service, ServerConfig, ClientConfig};
+use karyon_jsonrpc::{Error, Server, Client, rpc_impl};
struct HelloWorld {}
+#[rpc_impl]
impl HelloWorld {
- async fn say_hello(&self, params: Value) -> Result<Value, JsonRPCError> {
+ async fn say_hello(&self, params: Value) -> Result<Value, Error> {
let msg: String = serde_json::from_value(params)?;
Ok(serde_json::json!(format!("Hello {msg}!")))
}
-}
-let ex = Arc::new(smol::Executor::new());
+ async fn foo(&self, params: Value) -> Result<Value, Error> {
+ Ok(serde_json::json!("foo!"))
+ }
-//////////////////
-// Server
-//////////////////
-// Creates a new server
-let listener = TcpListener::bind("127.0.0.1:60000").await.unwrap();
-let config = ServerConfig::default();
-let server = Server::new(listener, config, ex.clone());
+ async fn bar(&self, params: Value) -> Result<Value, Error> {
+ Ok(serde_json::json!("bar!"))
+ }
+}
-// Register the HelloWorld service
-register_service!(HelloWorld, say_hello);
-server.attach_service(HelloWorld{});
+// Server
+async {
+ // Creates a new server
+ let server = Server::builder("tcp://127.0.0.1:60000")
+ .expect("create new server builder")
+ .service(HelloWorld{})
+ .build()
+ .await
+ .expect("build the server");
-// Starts the server
-ex.run(server.start());
+ // Starts the server
+ server.start().await.expect("start the server");
+};
-//////////////////
-// Client
-//////////////////
-// Creates a new client
-let conn = TcpStream::connect("127.0.0.1:60000").await.unwrap();
-let config = ClientConfig::default();
-let client = Client::new(conn, config);
+// Client
+async {
+ // Creates a new client
+ let client = Client::builder("tcp://127.0.0.1:60000")
+ .expect("create new client builder")
+ .build()
+ .await
+ .expect("build the client");
-let result: String = client.call("HelloWorld.say_hello", "world".to_string()).await.unwrap();
+ let result: String = client.call("HelloWorld.say_hello", "world".to_string())
+ .await
+ .expect("send a request");
+};
```
+
+
+
+