-
Notifications
You must be signed in to change notification settings - Fork 11
/
pubsub.rs
69 lines (59 loc) · 2.11 KB
/
pubsub.rs
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
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::error::Error;
use wamp_async::{Client, ClientConfig};
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();
// Connect to the server
let (mut client, (evt_loop, _rpc_evt_queue)) = Client::connect(
"wss://localhost:8080/ws",
Some(ClientConfig::default().set_ssl_verify(false)),
)
.await?;
println!("Connected !!");
// Spawn the event loop
tokio::spawn(evt_loop);
println!("Joining realm");
client.join_realm("realm1").await?;
let max_events = 10;
let mut cur_event_num: usize = 0;
// If one of the args is "pub", start as a publisher
if let Some(_) = std::env::args().find(|a| a == "pub") {
loop {
match client.publish("peer.heartbeat", None, None, true).await {
Ok(pub_id) => println!("\tSent event id {}", pub_id.unwrap()),
Err(e) => {
println!("publish error {}", e);
break;
}
};
cur_event_num += 1;
// Exit before sleeping
if cur_event_num >= max_events {
break;
}
tokio::time::sleep(std::time::Duration::from_secs(1)).await
}
// Start as a subscriber
} else {
println!(
"Subscribing to peer.heartbeat events. Start another instance with a 'pub' argument"
);
let (sub_id, mut heartbeat_queue) = client.subscribe("peer.heartbeat").await?;
println!("Waiting for {} heartbeats...", max_events);
while cur_event_num < max_events {
match heartbeat_queue.recv().await {
Some((pub_id, args, kwargs)) => {
println!("\tGot {} (args: {:?}, kwargs: {:?})", pub_id, args, kwargs)
}
None => println!("Subscription is done"),
};
cur_event_num += 1;
}
client.unsubscribe(sub_id).await?;
}
println!("Leaving realm");
client.leave_realm().await?;
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
client.disconnect().await;
Ok(())
}