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对象全局,我只能在发布函数中创建它。
我遇到了这个问题,并通过在connect_options.h中的set_user_name和set_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;
}
请试试:
auto connOpts = mqtt::connect_options_builder()
.clean_session()
.user_name("User name here") // put username here
.password(binary_ref_passwd)
.will(mqtt::message(TOPIC, LWT_PAYLOAD, QOS))
.finalize();
相关文章:
- 如何在 c++ 中将密码和用户名保存到 .txt 文件中.如果用户尝试登录,我仍然希望能够检索它们
- 当PSO细粒度策略对使用AdsGetObject MSDN API的Windows操作系统生效时,如何获取用户密码到期日
- mysql c ++连接器异常:用户"root"@'localhost'的访问被拒绝(使用密码:NO)&&MySQL服务器已消失
- 即使密码/用户无效,程序仍会继续
- 如何制作一个从文件中读取密码并与用户编写的密码进行比较的 c++ 程序
- 使用 C++ 和 cURL,如何通过需要用户 ID 和密码的 url 获取图像
- 如何使用用户定义的密码初始化AES-256密钥
- 当用户更改密码时,哪个窗口 API 调用?钩住这个 API 的好方法是什么
- 如何让我的checkLength函数循环,直到用户输入超过5个字符的密码
- QFile和QTextStream帮助(为具有用户名、密码和名称的用户编写ID)
- C# 或 C++ 如何使程序在询问用户密码时运行
- IM 创建一个 C++ 程序,其中用户可以尝试 2 次尝试密码用户名组合.如果他们无法获得它,他们的程序就会停止
- 仅使用一个用户id和密码访问数据库
- 正在获取当前用户的密码文件条目
- 如何处理pam_authenticate对有效用户和密码的PAM_AUTHTOK_RECOVERY_ERR返回值
- 在c++的sqlite中验证用户的有效密码
- Paho MQTT c++连接用户和密码
- 如何使用LDAP对用户进行密码身份验证
- 使用用户密码加密
- C++凯撒密码跳过用户输入中的第一个单词