forked from AtherEnergy/rumqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaypause.rs
42 lines (32 loc) · 1.07 KB
/
playpause.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
use rumqtt::{MqttClient, MqttOptions, QoS};
use std::{thread, time::Duration};
fn main() {
pretty_env_logger::init();
let mqtt_options = MqttOptions::new("test-id", "127.0.0.1", 1883).set_keep_alive(10);
let (mut mqtt_client, notifications) = MqttClient::start(mqtt_options).unwrap();
mqtt_client.subscribe("hello/world", QoS::AtLeastOnce).unwrap();
let mut c1 = mqtt_client.clone();
let mut c2 = mqtt_client.clone();
thread::spawn(move || {
let dur = Duration::new(1, 0);
for i in 0..100 {
let payload = format!("publish {}", i);
thread::sleep(dur);
c1.publish("hello/world", QoS::AtLeastOnce, false, payload).unwrap();
}
});
thread::spawn(move || {
let dur = Duration::new(5, 0);
for i in 0..100 {
if i % 2 == 0 {
c2.pause().unwrap();
} else {
c2.resume().unwrap();
}
thread::sleep(dur);
}
});
for notification in notifications {
println!("{:?}", notification)
}
}