pub struct Callbacks<'a, T> {
pub data: T,
/* private fields */
}Expand description
Handling mosquitto callbacks. This will pass a mutable reference to the contained data to the callbacks.
Fields§
§data: TImplementations§
Source§impl<'a, T> Callbacks<'a, T>
impl<'a, T> Callbacks<'a, T>
Sourcepub fn new(mosq: &Mosquitto, data: T) -> Callbacks<'_, T>
pub fn new(mosq: &Mosquitto, data: T) -> Callbacks<'_, T>
create a new callback handler with data. Initialize with an existing Mosquitto reference.
Sourcepub fn on_message<C: Fn(&mut T, MosqMessage) + 'a>(&mut self, callback: C)
pub fn on_message<C: Fn(&mut T, MosqMessage) + 'a>(&mut self, callback: C)
provide a closure which will be called when messages arrive. You are passed a mutable reference to data and the message
Examples found in repository?
4fn main() {
5 let m = Mosquitto::new("test");
6
7 m.will_set("test/will",b"finished!",0,false).expect("can't set will");
8
9 m.connect("localhost",1883).expect("can't connect");
10 let bonzo = m.subscribe("bonzo/#",0).expect("can't subscribe to bonzo");
11 let frodo = m.subscribe("frodo/#",0).expect("can't subscribe to frodo");
12
13 // not interested in any retained messages!
14 let mut mc = m.callbacks(());
15 mc.on_message(|_,msg| {
16 if ! msg.retained() {
17 if bonzo.matches(&msg) {
18 println!("bonzo {:?}",msg);
19 } else
20 if frodo.matches(&msg) {
21 println!("frodo {:?}",msg);
22 m.disconnect().unwrap();
23 }
24 }
25 });
26
27 m.loop_forever(200).expect("broken loop");
28}More examples
5fn main() {
6 let m = Mosquitto::new("test");
7
8 m.connect("localhost",1883).expect("can't connect");
9 m.subscribe("bilbo/#",1).expect("can't subscribe to bonzo");
10
11 let mt = m.clone();
12 thread::spawn(move || {
13 let timeout = time::Duration::from_millis(500);
14 for _ in 0..5 {
15 mt.publish("bilbo/baggins","hello dolly".as_bytes(), 1, false).unwrap();
16 thread::sleep(timeout);
17 }
18 mt.disconnect().unwrap();
19 });
20
21 let mut mc = m.callbacks(0);
22 mc.on_message(|data,msg| {
23 println!("bilbo {:?}",msg);
24 *data += 1;
25 });
26 mc.on_disconnect(|_,rc| println!("disconnect {}",rc));
27
28
29 m.loop_until_disconnect(200).expect("broken loop");
30 println!("received {} messages",mc.data);
31}Sourcepub fn on_connect<C: Fn(&mut T, i32) + 'a>(&mut self, callback: C)
pub fn on_connect<C: Fn(&mut T, i32) + 'a>(&mut self, callback: C)
provide a closure which is called when connection happens. You are passed a mutable reference to data and the status.
Sourcepub fn on_publish<C: Fn(&mut T, i32) + 'a>(&mut self, callback: C)
pub fn on_publish<C: Fn(&mut T, i32) + 'a>(&mut self, callback: C)
provide a closure which is called after publishing a message. You are passed a mutable reference to data and the message id.
Examples found in repository?
6fn go() -> Result<(),Box<Error>> {
7 let m = Mosquitto::new("test");
8
9 m.connect("localhost",1883)?;
10
11 // publish and get a message id
12 let our_mid = m.publish("bonzo/dog","hello dolly".as_bytes(), 2, false)?;
13
14 // and wait for confirmation for that message id
15 let mut mc = m.callbacks(());
16 mc.on_publish(|_,mid| {
17 if mid == our_mid {
18 m.disconnect().unwrap();
19 }
20 });
21
22 // wait forever until explicit disconnect
23 m.loop_until_disconnect(-1)?;
24 Ok(())
25}Sourcepub fn on_subscribe<C: Fn(&mut T, i32) + 'a>(&mut self, callback: C)
pub fn on_subscribe<C: Fn(&mut T, i32) + 'a>(&mut self, callback: C)
provide a closure which is called after subscribing. You are passed a mutable reference to data and the subscription id.
Sourcepub fn on_unsubscribe<C: Fn(&mut T, i32) + 'a>(&mut self, callback: C)
pub fn on_unsubscribe<C: Fn(&mut T, i32) + 'a>(&mut self, callback: C)
provide a closure which is called after unsubscribing from a topic You are passed a mutable reference to data and the subscription id.
Sourcepub fn on_disconnect<C: Fn(&mut T, i32) + 'a>(&mut self, callback: C)
pub fn on_disconnect<C: Fn(&mut T, i32) + 'a>(&mut self, callback: C)
provide a closure which is called when client disconnects from broker. You are passed a mutable reference to data and ….
Examples found in repository?
5fn main() {
6 let m = Mosquitto::new("test");
7
8 m.connect("localhost",1883).expect("can't connect");
9 m.subscribe("bilbo/#",1).expect("can't subscribe to bonzo");
10
11 let mt = m.clone();
12 thread::spawn(move || {
13 let timeout = time::Duration::from_millis(500);
14 for _ in 0..5 {
15 mt.publish("bilbo/baggins","hello dolly".as_bytes(), 1, false).unwrap();
16 thread::sleep(timeout);
17 }
18 mt.disconnect().unwrap();
19 });
20
21 let mut mc = m.callbacks(0);
22 mc.on_message(|data,msg| {
23 println!("bilbo {:?}",msg);
24 *data += 1;
25 });
26 mc.on_disconnect(|_,rc| println!("disconnect {}",rc));
27
28
29 m.loop_until_disconnect(200).expect("broken loop");
30 println!("received {} messages",mc.data);
31}