MosqMessage

Struct MosqMessage 

Source
pub struct MosqMessage { /* private fields */ }
Expand description

A mosquitto message

Implementations§

Source§

impl MosqMessage

Source

pub fn topic(&self) -> &str

the topic of the message. This will panic if the topic isn’t valid UTF-8

Examples found in repository?
examples/self-publish-receive-many.rs (line 22)
5fn run() -> mosq::Result<()> {
6    let m = Mosquitto::new("test");
7
8    m.connect_wait("localhost",1883,300)?;
9    let bilbo = m.subscribe("bilbo/#",1)?;
10
11    let mt = m.clone();
12    thread::spawn(move || {
13        for i in 0..5 {
14            let topic = format!("bilbo/{}",10*(i+1));
15            let data = format!("hello #{}",i);
16            mt.publish(&topic,data.as_bytes(), 1, false).unwrap();
17        }
18    });
19
20    let msgs = bilbo.receive_many(300)?;
21    for msg in msgs {
22        println!("topic {} text '{}'",msg.topic(),msg.text());
23    }
24    Ok(())
25}
Source

pub fn payload(&self) -> &[u8]

the payload as bytes

Source

pub fn text(&self) -> &str

the payload as text. This will panic if the payload was not valid UTF-8

Examples found in repository?
examples/self-publish-receive-many.rs (line 22)
5fn run() -> mosq::Result<()> {
6    let m = Mosquitto::new("test");
7
8    m.connect_wait("localhost",1883,300)?;
9    let bilbo = m.subscribe("bilbo/#",1)?;
10
11    let mt = m.clone();
12    thread::spawn(move || {
13        for i in 0..5 {
14            let topic = format!("bilbo/{}",10*(i+1));
15            let data = format!("hello #{}",i);
16            mt.publish(&topic,data.as_bytes(), 1, false).unwrap();
17        }
18    });
19
20    let msgs = bilbo.receive_many(300)?;
21    for msg in msgs {
22        println!("topic {} text '{}'",msg.topic(),msg.text());
23    }
24    Ok(())
25}
Source

pub fn qos(&self) -> u32

the quality-of-service of the message. The desired QoS is specified when we subscribe.

Source

pub fn retained(&self) -> bool

was the message retained by the broker? True if we received this as a retained message. Subsequent messages marked as retained will not set this.

Examples found in repository?
examples/subscribe-and-listen.rs (line 16)
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}

Trait Implementations§

Source§

impl Clone for MosqMessage

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MosqMessage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for MosqMessage

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.