簡體   English   中英

Paho MQTT C ++連接用戶和密碼

[英]Paho MQTT C++ connection user and password

我正在實現一個客戶端以通過MQTT發送數據,並且正在使用Paho MQTT c ++庫。 現在,我需要添加對用戶和密碼身份驗證的支持,並在嘗試像這樣設置它們時:

std::string user = "user";
std::string password = "password";
mqtt::connect_options connOpts;
connOpts.set_user_name(user);
connOpts.set_password(password);

我得到

對mqtt :: connect_options :: set_user_name(std:string const&)的未定義引用

但在頭文件connection_options.h中

/**
 * Sets the user name to use for the connection.
 * @param userName 
 */
void set_user_name(const std::string& userName);

set_password(password)也會發生同樣的事情;

我遇到的另一個問題是,由於無法在類中全局設置mqtt :: async_client對象,因此無法保持連接狀態,我只能在publish函數內創建它。

提前致謝。

我遇到了這個問題,並通過在connect_options.h中的set_user_nameset_password函數中添加我自己的代碼(在C ++包裝程序的任何其他文件中未初始化)中解決了此問題。

void set_user_name(const std::string& userName){
    const char * usr = userName.c_str();
    opts_.username = usr;
}


void set_password(const std::string& password){
    const char * pw = password.c_str();
    opts_.password = pw;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM