aboutsummaryrefslogtreecommitdiff
path: root/jsonrpc/examples/pubsub_client.rs
blob: bb3ce4c70c34a61b4f261cca53902a46938f2bfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::time::Duration;

use log::info;
use serde::{Deserialize, Serialize};
use smol::Timer;

use karyon_jsonrpc::Client;

#[derive(Deserialize, Serialize, Debug)]
struct Pong {}

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 sub = client
        .subscribe("Calc.log_subscribe", ())
        .await
        .expect("Subscribe to log_subscribe method");

    smol::spawn(async move {
        loop {
            let m = sub.recv().await.expect("Receive new log msg");
            info!("Receive new log {m}");
        }
    })
    .detach();

    loop {
        Timer::after(Duration::from_millis(500)).await;
        let _: Pong = client
            .call("Calc.ping", ())
            .await
            .expect("Send ping request");
    }
}

fn main() {
    env_logger::init();
    smol::future::block_on(async {
        smol::spawn(run_client()).await;
    });
}