Guest User

Untitled

a guest
Jul 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. extern crate mqttc;
  2. extern crate netopt;
  3. extern crate ws;
  4.  
  5. use mqttc::{PubOpt, PubSub};
  6. use std::{thread, time};
  7.  
  8. static USERNAME: &'static str = "";
  9. static PASSWORD: &'static str = "";
  10.  
  11.  
  12. fn new_client() -> mqttc::Client {
  13. use mqttc::{ClientOptions, ReconnectMethod};
  14. use netopt::{NetworkOptions, SslContext};
  15.  
  16. // Using ssl network connection
  17. let mut netopt = NetworkOptions::new();
  18. let ssl = SslContext::default();
  19. netopt.tls(ssl);
  20.  
  21. // Using credentials for client
  22. let mut opts = ClientOptions::new();
  23. opts.set_username(USERNAME.to_string());
  24. opts.set_password(PASSWORD.to_string());
  25. let timeout = time::Duration::from_secs(1_0000);
  26. opts.set_reconnect(ReconnectMethod::ReconnectAfter(timeout));
  27.  
  28. opts.connect("178.128.184.101:8883", netopt).unwrap()
  29. }
  30.  
  31. fn listen_for_messages() {
  32. let mut client = new_client();
  33. client.subscribe("+/outbound").ok();
  34.  
  35. loop {
  36. match client.await() {
  37. Ok(Some(message)) => println!("message = {:?}", message),
  38. Err(_) | Ok(_) => (),
  39. }
  40. }
  41. }
  42.  
  43. fn main() {
  44. thread::spawn(listen_for_messages);
  45.  
  46. let mut client = new_client();
  47. loop {
  48. let mut line = String::new();
  49. std::io::stdin().read_line(&mut line).ok();
  50. line.pop();
  51. let line = line.as_str();
  52.  
  53. client
  54. .publish("my-device/outbound", line, PubOpt::at_least_once())
  55. .ok();
  56. }
  57. }
Add Comment
Please, Sign In to add comment