-
Notifications
You must be signed in to change notification settings - Fork 227
/
websocket.rs
64 lines (55 loc) · 1.68 KB
/
websocket.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
#[cfg(feature = "websocket")]
use rumqttc::{self, AsyncClient, MqttOptions, QoS, Transport};
#[cfg(feature = "websocket")]
use std::{error::Error, time::Duration};
#[cfg(feature = "websocket")]
use tokio::{task, time};
#[cfg(feature = "websocket")]
#[tokio::main(worker_threads = 1)]
async fn main() -> Result<(), Box<dyn Error>> {
pretty_env_logger::init();
// port parameter is ignored when scheme is websocket
let mut mqttoptions = MqttOptions::new(
"clientId-aSziq39Bp3",
"ws://broker.mqttdashboard.com:8000/mqtt",
8000,
);
mqttoptions.set_transport(Transport::Ws);
mqttoptions.set_keep_alive(Duration::from_secs(60));
let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
task::spawn(async move {
requests(client).await;
time::sleep(Duration::from_secs(3)).await;
});
loop {
let event = eventloop.poll().await;
match event {
Ok(notif) => {
println!("Event = {:?}", notif);
}
Err(err) => {
println!("Error = {:?}", err);
return Ok(());
}
}
}
}
#[cfg(feature = "websocket")]
async fn requests(client: AsyncClient) {
client
.subscribe("hello/world", QoS::AtMostOnce)
.await
.unwrap();
for i in 1..=10 {
client
.publish("hello/world", QoS::ExactlyOnce, false, vec![1; i])
.await
.unwrap();
time::sleep(Duration::from_secs(1)).await;
}
time::sleep(Duration::from_secs(120)).await;
}
#[cfg(not(feature = "websocket"))]
fn main() {
panic!("Enable websocket feature with `--features=websocket`");
}