Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate mqttc;
- extern crate netopt;
- extern crate ws;
- use mqttc::{PubOpt, PubSub};
- use std::{thread, time};
- static USERNAME: &'static str = "";
- static PASSWORD: &'static str = "";
- fn new_client() -> mqttc::Client {
- use mqttc::{ClientOptions, ReconnectMethod};
- use netopt::{NetworkOptions, SslContext};
- // Using ssl network connection
- let mut netopt = NetworkOptions::new();
- let ssl = SslContext::default();
- netopt.tls(ssl);
- // Using credentials for client
- let mut opts = ClientOptions::new();
- opts.set_username(USERNAME.to_string());
- opts.set_password(PASSWORD.to_string());
- let timeout = time::Duration::from_secs(1_0000);
- opts.set_reconnect(ReconnectMethod::ReconnectAfter(timeout));
- opts.connect("178.128.184.101:8883", netopt).unwrap()
- }
- fn listen_for_messages() {
- let mut client = new_client();
- client.subscribe("+/outbound").ok();
- loop {
- match client.await() {
- Ok(Some(message)) => println!("message = {:?}", message),
- Err(_) | Ok(_) => (),
- }
- }
- }
- fn main() {
- thread::spawn(listen_for_messages);
- let mut client = new_client();
- loop {
- let mut line = String::new();
- std::io::stdin().read_line(&mut line).ok();
- line.pop();
- let line = line.as_str();
- client
- .publish("my-device/outbound", line, PubOpt::at_least_once())
- .ok();
- }
- }
Add Comment
Please, Sign In to add comment