forked from AtherEnergy/rumqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpconnect.rs
46 lines (36 loc) · 1.33 KB
/
httpconnect.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 rumqtt::{MqttClient, MqttOptions, Proxy, QoS, ReconnectOptions};
use serde_derive::Deserialize;
use std::thread;
use std::time::Duration;
#[derive(Deserialize, Debug)]
struct Config {
proxy_host: String,
proxy_port: u16,
main_host: String,
main_port: u16,
}
fn main() {
pretty_env_logger::init();
let config: Config = envy::from_env().unwrap();
let key = include_bytes!("tlsfiles/server.key.pem");
let reconnect_options = ReconnectOptions::AfterFirstSuccess(10);
let proxy = Proxy::HttpConnect(config.proxy_host, config.proxy_port, key.to_vec(), 40);
let id = "http-connect-test";
let mqtt_options = MqttOptions::new(id, config.main_host, config.main_port);
let mqtt_options = mqtt_options
.set_keep_alive(10)
.set_reconnect_opts(reconnect_options)
.set_proxy(proxy);
let (mut mqtt_client, notifications) = MqttClient::start(mqtt_options).unwrap();
mqtt_client.subscribe("hello/world", QoS::AtLeastOnce).unwrap();
thread::spawn(move || {
for i in 0..100 {
let payload = format!("publish {}", i);
thread::sleep(Duration::from_millis(100));
mqtt_client.publish("hello/world", QoS::AtLeastOnce, false, payload).unwrap();
}
});
for notification in notifications {
println!("{:?}", notification)
}
}