inre / rust-mq

RustMQ is the MQTT client written on pure Rust.
MIT License
190 stars 28 forks source link

Comparing one TopicPath to another #10

Closed dwillie closed 8 years ago

dwillie commented 8 years ago

I just wanted to check if you provide a method or have a recommendation on, how one would best go about ensuring – when a client is subscribed to multiple topics – which topic the incoming message matches to?

Eg. My client is subscribed to "/stuff/#" and "/things/awesome_things/#" and receives a message with the topic "/things/awesome_things/sandwich". How would I check which subscription topic the incoming message related to?

Sorry if that's a bit convoluted!

inre commented 8 years ago

There is no simple way. Only dirty solution:

let topic = message.topic.path.as_str();
if topic.len() >= 3 && &topic[..7] == "/stuff/" {
  println!("subscribed to /stuff/#");
}
if topic.len() >= 23 && &topic[..23] == "/things/awesome_things/" {
   println!("subscribed to /things/awesome_things/#");
}

The MQTT protocol doesn't provide any help for this.