Skip to content

Instantly share code, notes, and snippets.

@jan-schreib
Created May 26, 2020 15:40
Show Gist options
  • Save jan-schreib/dc0a8499fc53349c860611568a7f5850 to your computer and use it in GitHub Desktop.
Save jan-schreib/dc0a8499fc53349c860611568a7f5850 to your computer and use it in GitHub Desktop.
extern crate mosquitto_client as mosq;
use mosq::Mosquitto;
use std::{thread, time};
fn main() {
let m = Mosquitto::new("test");
m.connect("localhost",1883).expect("can't connect");
m.subscribe("bilbo/#",1).expect("can't subscribe to bonzo");
let mt = m.clone();
thread::spawn(move || {
let timeout = time::Duration::from_millis(500);
for _ in 0..5 {
mt.publish("bilbo/baggins","hello dolly".as_bytes(), 1, false).unwrap();
thread::sleep(timeout);
}
mt.disconnect().unwrap();
});
let mut mc = m.callbacks(0);
mc.on_message(|data,msg| {
println!("bilbo {:?}",msg);
*data += 1;
});
m.loop_until_disconnect(200).expect("broken loop");
println!("received {} messages",mc.data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment