From a06239ccc5e21fd20182ec3046cf9174ecc58a43 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Thu, 30 May 2024 02:07:50 +0200 Subject: jsonrpc/server: use queue with condvar instead of async channels --- jsonrpc/examples/pubsub_client.rs | 68 +++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 34 deletions(-) (limited to 'jsonrpc/examples/pubsub_client.rs') diff --git a/jsonrpc/examples/pubsub_client.rs b/jsonrpc/examples/pubsub_client.rs index fee2a26..830b32f 100644 --- a/jsonrpc/examples/pubsub_client.rs +++ b/jsonrpc/examples/pubsub_client.rs @@ -1,47 +1,47 @@ +use std::time::Duration; + use serde::{Deserialize, Serialize}; -use smol::stream::StreamExt; +use smol::Timer; use karyon_jsonrpc::Client; #[derive(Deserialize, Serialize, Debug)] struct Pong {} -fn main() { - env_logger::init(); - smol::future::block_on(async { - let client = Client::builder("tcp://127.0.0.1:6000") - .expect("Create client builder") - .build() - .await - .expect("Build a client"); - - let result: Pong = client +async fn run_client() { + let client = Client::builder("tcp://127.0.0.1:6000") + .expect("Create client builder") + .build() + .await + .expect("Build a client"); + + let clientc = client.clone(); + smol::spawn(async move {}).detach(); + + let (_, sub) = client + .subscribe("Calc.log_subscribe", ()) + .await + .expect("Subscribe to log_subscribe method"); + + smol::spawn(async move { + loop { + let _m = sub.recv().await.unwrap(); + } + }) + .detach(); + + loop { + Timer::after(Duration::from_secs(1)).await; + let _: Pong = clientc .call("Calc.ping", ()) .await .expect("Send ping request"); + } +} - println!("receive pong msg: {:?}", result); - - let (sub_id, sub) = client - .subscribe("Calc.log_subscribe", ()) - .await - .expect("Subscribe to log_subscribe method"); - - smol::spawn(async move { - sub.for_each(|m| { - println!("Receive new notification: {m}"); - }) - .await - }) - .detach(); - - smol::Timer::after(std::time::Duration::from_secs(5)).await; - - client - .unsubscribe("Calc.log_unsubscribe", sub_id) - .await - .expect("Unsubscribe from log_unsubscirbe method"); - - smol::Timer::after(std::time::Duration::from_secs(2)).await; +fn main() { + env_logger::init(); + smol::future::block_on(async { + smol::spawn(run_client()).await; }); } -- cgit v1.2.3