-
Notifications
You must be signed in to change notification settings - Fork 227
/
syncpubsub_v5.rs
46 lines (38 loc) · 1.42 KB
/
syncpubsub_v5.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
use rumqttc::v5::mqttbytes::{LastWill, QoS};
use rumqttc::v5::ConnectionError;
use rumqttc::v5::{Client, MqttOptions};
use std::thread;
use std::time::Duration;
fn main() {
pretty_env_logger::init();
let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1884);
let will = LastWill::new("hello/world", "good bye", QoS::AtMostOnce, false);
mqttoptions
.set_keep_alive(Duration::from_secs(5))
.set_last_will(will);
let (client, mut connection) = Client::new(mqttoptions, 10);
thread::spawn(move || publish(client));
for (i, notification) in connection.iter().enumerate() {
match notification {
Err(ConnectionError::Io(error))
if error.kind() == std::io::ErrorKind::ConnectionRefused =>
{
println!("Failed to connect to the server. Make sure correct client is configured properly!\nError: {:?}", error);
return;
}
_ => {}
}
println!("{}. Notification = {:?}", i, notification);
}
println!("Done with the stream!!");
}
fn publish(client: Client) {
client.subscribe("hello/+/world", QoS::AtMostOnce).unwrap();
for i in 0..10_usize {
let payload = vec![1; i];
let topic = format!("hello/{}/world", i);
let qos = QoS::AtLeastOnce;
let _ = client.publish(topic, qos, true, payload);
}
thread::sleep(Duration::from_secs(1));
}