Closed qxqxq closed 2 years ago
Use the ConnectOptionsBuilder: https://docs.rs/paho-mqtt/latest/paho_mqtt/connect_options/struct.ConnectOptionsBuilder.html#method.user_name
Thanks!
使用 ConnectOptionsBuilder:https ://docs.rs/paho-mqtt/latest/paho_mqtt/connect_options/struct.ConnectOptionsBuilder.html#method.user_name
fn get_mqtt_client(mqtt_config: MQTTConfig) -> Result<paho_mqtt::Client, paho_mqtt::Error> {
let uri = format!("tcp://{}:{}", mqtt_config.host, mqtt_config.port);
let opts = paho_mqtt::CreateOptionsBuilder::new()
.server_uri(uri)
.client_id(mqtt_config.client_id)
.mqtt_version(paho_mqtt::MQTT_VERSION_5)
// 持久化
.persistence(None)
.finalize();
let connect_opts = paho_mqtt::ConnectOptionsBuilder::new()
.user_name(mqtt_config.username)
.password(mqtt_config.password)
.keep_alive_interval(Duration::from_secs(60))
.automatic_reconnect(Duration::from_secs(1), Duration::from_secs(60))
.clean_session(true)
.finalize();
let client = paho_mqtt::Client::new(opts)?;
client.connect(connect_opts)?;
Ok(client)
}
There are no username and password parameters when creating client. What if user authentication is enabled on the server? Thanks!