aboutsummaryrefslogtreecommitdiff
path: root/jsonrpc/examples/client.rs
blob: 0f87ecbb11a2d1663bb2f1e15f9716ac7956cbb5 (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
47
48
49
50
51
52
53
54
55
56
use std::time::Duration;

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

use karyon_jsonrpc::Client;

#[derive(Deserialize, Serialize)]
struct Req {
    x: u32,
    y: u32,
}

#[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("Create rpc client");

        let params = Req { x: 10, y: 7 };
        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
            .expect("Call Calc.sub method");
        info!("Sub result: {result}");

        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);
        }
    });
}