aboutsummaryrefslogtreecommitdiff
path: root/jsonrpc/examples/pubsub_client.rs
diff options
context:
space:
mode:
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;
});
}