publish_wait/
publish_wait.rs

1extern crate mosquitto_client as mosq;
2use mosq::Mosquitto;
3use std::time::{Instant,Duration};
4
5// you would think that the stdlib would actually provide
6// a method to do this...
7fn as_millis(d: Duration) -> f64 {
8    1000.0*(d.as_secs() as f64) + (d.subsec_nanos() as f64)/1e6
9}
10
11const TIMEOUT: i32 = 300;
12
13fn run() -> Result<(),Box<std::error::Error>> {
14    let m = Mosquitto::new("test");
15
16    let t = Instant::now();
17
18    m.connect_wait("localhost",1883,TIMEOUT)?;
19    m.publish_wait("/bonzo/dog",b"hello dolly",2,false,TIMEOUT)?;
20    m.publish_wait("/bonzo/cat",b"meeeaaaww",2,false,TIMEOUT)?;
21    println!("elapsed {:.2} msec",as_millis(t.elapsed()));
22    Ok(())
23}
24
25fn main() {
26    run().expect("failed");
27}