微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Rust上使用paho-mqtt

如何解决在Rust上使用paho-mqtt

我在Crates.io中看到有一个已经实现MQTT的板条箱。但是,当尝试使用the repo中提供的示例时,我遇到了很多错误,并且在学习过程中,我正在考虑解决该问题。

添加

[dependencies]
paho-mqtt = "0.7.1"

我尝试使用main.rs进行构建:

use std::process;

extern crate paho_mqtt as mqtt;

fn main() {
    // Create a client & define connect options
    let cli = mqtt::Client::new("tcp://localhost:1883").unwrap_or_else(|err| {
        println!("Error creating the client: {:?}",err);
        process::exit(1);
    });

    let conn_opts = mqtt::ConnectOptionsBuilder::new()
        .keep_alive_interval(Duration::from_secs(20))
        .clean_session(true)
        .finalize();

    // Connect and wait for it to complete or fail
    if let Err(e) = cli.connect(conn_opts).wait() {
        println!("Unable to connect:\n\t{:?}",e);
        process::exit(1);
    }

    // Create a message and publish it
    let msg = mqtt::Message::new("test","Hello World!");
    let tok = cli.publish(msg);

    if let Err(e) = tok.wait() {
        println!("Error sending message: {:?}",e);
    }

    // disconnect from the broker
    let tok = cli.disconnect();
    tok.wait().unwrap();
}

我收到以下错误

error[E0433]: Failed to resolve: use of undeclared type or module `Duration`
  --> src/main.rs:13:30
   |
13 |         .keep_alive_interval(Duration::from_secs(20))
   |                              ^^^^^^^^ use of undeclared type or module `Duration`

error[E0599]: no method named `wait` found for enum `std::result::Result<mqtt::ServerResponse,mqtt::MqttError>` in the current scope
  --> src/main.rs:18:44
   |
18 |     if let Err(e) = cli.connect(conn_opts).wait() {
   |                                            ^^^^ method not found in `std::result::Result<mqtt::ServerResponse,mqtt::MqttError>`

error[E0061]: this function takes 3 arguments but 2 arguments were supplied
  --> src/main.rs:24:15
   |
24 |     let msg = mqtt::Message::new("test","Hello World!");
   |               ^^^^^^^^^^^^^^^^^^ ------  -------------- supplied 2 arguments
   |               |
   |               expected 3 arguments

error[E0599]: no method named `wait` found for enum `std::result::Result<(),mqtt::MqttError>` in the current scope
  --> src/main.rs:27:25
   |
27 |     if let Err(e) = tok.wait() {
   |                         ^^^^ method not found in `std::result::Result<(),mqtt::MqttError>`

error[E0061]: this function takes 1 argument but 0 arguments were supplied
  --> src/main.rs:32:19
   |
32 |     let tok = cli.disconnect();
   |                   ^^^^^^^^^^- supplied 0 arguments
   |                   |
   |                   expected 1 argument

error[E0599]: no method named `wait` found for enum `std::result::Result<(),mqtt::MqttError>` in the current scope
  --> src/main.rs:33:9
   |
33 |     tok.wait().unwrap();
   |         ^^^^ method not found in `std::result::Result<(),mqtt::MqttError>`

error: aborting due to 6 prevIoUs errors

我可以使用13

解决std::time::Duration行中的错误

24行中的错误,我可以通过添加QoS

解决

我还缺少哪些其他错误

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。