Cannot connect to mosquitto

extern crate mosquitto_client_wrapper as mosq;
use mosq::Mosquitto;
use std::{thread, time};

fn main() {
    let m = Mosquitto::new("test").unwrap();

    m.connect("localhost", 1883, 5).expect("can't connect");
    m.subscribe("bonzo/topic", 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_connect(|t, i| {
        println!("connection:t: {}, i: {}", t, i);//here I'm getting 0, meaning that connection was unsuccessfull
    });
    // mc.on_message(|data, msg| {
    //     println!("on_message {:?}", msg);
    //     *data += 1;
    // });
    mc.on_subscribe(|data, msg| {
        println!("on_subscribe {:?}", msg);
        *data += 1;
    });

    m.loop_until_disconnect(200).expect("broken loop");
    println!("received {} messages", mc.data);
}

Can anyone help with that? What else is needed to successfully connect?
Thank you

Are you seeing any error(s)?

1 Like