aboutsummaryrefslogtreecommitdiff
path: root/jsonrpc/examples/pubsub_client.rs
diff options
context:
space:
mode:
authorhozan23 <hozan23@karyontech.net>2024-05-30 02:07:50 +0200
committerhozan23 <hozan23@karyontech.net>2024-05-30 02:07:50 +0200
commita06239ccc5e21fd20182ec3046cf9174ecc58a43 (patch)
tree12bf68067ba377f8bb081e98c3e3a4ac4fcd13b7 /jsonrpc/examples/pubsub_client.rs
parent34f528a3b4063b4a25915e60d7f22ee1fb2d1bd9 (diff)
jsonrpc/server: use queue with condvar instead of async channels
Diffstat (limited to 'jsonrpc/examples/pubsub_client.rs')
-rw-r--r--jsonrpc/examples/pubsub_client.rs68
1 files changed, 34 insertions, 34 deletions
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;
});
}