From 3429caa87699d986f799a11f6e0f4526e723b655 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Fri, 14 Jun 2024 22:49:53 +0200 Subject: jsonrpc: client use unbounded channels as buffer for sending requests & clean up examples --- jsonrpc/examples/client.rs | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) (limited to 'jsonrpc/examples/client.rs') diff --git a/jsonrpc/examples/client.rs b/jsonrpc/examples/client.rs index 662aacb..0f87ecb 100644 --- a/jsonrpc/examples/client.rs +++ b/jsonrpc/examples/client.rs @@ -1,5 +1,6 @@ use std::time::Duration; +use log::info; use serde::{Deserialize, Serialize}; use smol::Timer; @@ -21,29 +22,35 @@ fn main() { .expect("Create client builder") .build() .await - .unwrap(); - - let clientc = client.clone(); - smol::spawn(async move { - loop { - Timer::after(Duration::from_millis(500)).await; - let result: Pong = clientc.call("Calc.ping", ()).await.unwrap(); - println!("ping msg result: {:?}", result); - } - }) - .detach(); + .expect("Create rpc client"); let params = Req { x: 10, y: 7 }; - let result: u32 = client.call("Calc.add", params).await.unwrap(); - println!("result {result}"); + let result: u32 = client + .call("Calc.add", params) + .await + .expect("Call Calc.add method"); + info!("Add result: {result}"); let params = Req { x: 10, y: 7 }; - let result: u32 = client.call("Calc.sub", params).await.unwrap(); - println!("result {result}"); - - let result: String = client.call("Calc.version", ()).await.unwrap(); - println!("result {result}"); + let result: u32 = client + .call("Calc.sub", params) + .await + .expect("Call Calc.sub method"); + info!("Sub result: {result}"); - Timer::after(Duration::from_secs(10)).await; + let result: String = client + .call("Calc.version", ()) + .await + .expect("Call Calc.version method"); + info!("Version result: {result}"); + + loop { + Timer::after(Duration::from_millis(100)).await; + let result: Pong = client + .call("Calc.ping", ()) + .await + .expect("Call Calc.ping method"); + info!("Ping result: {:?}", result); + } }); } -- cgit v1.2.3