/// Connect MQTT client to the broker
///
/// Used for sending connect request to the broker after Client has been initialized. This method can be called multiple times
/// during the session if needed (eg. refreshing authentication parameters).
///
Connect MQTT client to the broker
Used for sending connect request to the broker after Client has been initialized. This method can be called multiple times
during the session if needed .
Connect MQTT client to the broker Used for sending connect request to the broker after Client has been initialized . This method can be called multiple times during the session if needed .
pub async fn connect(
        &mut self,
        keep_alive: u16,
        client_id: &str,
        username: Option<&str>,
        password: Option<&[u8]>,
        clean_session: bool,
    ) -> Result<&Self, MqttError> {
        info!(
            "--Connect-- client {} username: {:?} password: {:?}",
            client_id, username, password
        );
        // Calculate the exact size 10 bytes +2 for password length for the basic header + client_id, username & password
        //let mut raw_buf = Vec::with_capacity(16 + client_id.len() + username.map_or(0, |s| s.len()+1) + password.map_or(0, |s| s.len()+1));
        let mut raw_buf = [0 as u8; 1024];
        self.send_packet(|_| {
            // Encode an MQTT Connect packet.
            let pkt = Connect {
                protocol: mqttrs::Protocol::MQTT311,
                keep_alive,
                client_id,
                clean_session,
                last_will: None,
                username,
                password,
            }
            .into();
            // Keeping encoding as hard error as there is nothing on runtime that can be done to fix this. This should crash and hard
            let length =
                mqttrs::encode_slice(&pkt, &mut raw_buf).expect("Encoding connect packet failed");
            let buf = BytesMut::from(&raw_buf[..length]);
            // Magic number for Connect request as there are no Pids for them.
            let msg = MqttMsg::new((0 as u16).into(), buf);
            let fut = msg.get_packet_future();
            let channel_packet = ChannelPacket::Connect(msg, keep_alive);
            Ok((channel_packet, fut, 0))
        })
        .await?;
        info!("Connect done");
        Ok(self)
    }
pub async fn connect ( & mut self , keep_alive : u16 , client_id : & str , username : Option < & str > , password : Option < & [ u8 ] > , clean_session : bool , ) -> Result < & Self , MqttError > { info ! ( " " client_id username password ) ; // Calculate the exact size 10 bytes +2 for password length for the basic header + client_id, username & password //let mut raw_buf = Vec::with_capacity(16 + client_id.len() + username.map_or(0, |s| s.len()+1) + password.map_or(0, |s| s.len()+1)); let mut raw_buf = [ 0 as u8 ; 1024 ] ; self . send_packet ( | _ | { // Encode an MQTT Connect packet. let pkt = Connect { protocol : mqttrs :: Protocol :: MQTT311 , keep_alive , client_id , clean_session , last_will : None , username , password , } . into ( ) ; // Keeping encoding as hard error as there is nothing on runtime that can be done to fix this. This should crash and hard let length = mqttrs :: encode_slice ( & pkt , & mut raw_buf ) . expect ( " " ) ; let buf = BytesMut :: from ( & raw_buf [ .. length ] ) ; // Magic number for Connect request as there are no Pids for them. let msg = MqttMsg :: new ( ( 0 as u16 ) . into ( ) , buf ) ; let fut = msg . get_packet_future ( ) ; let channel_packet = ChannelPacket :: Connect ( msg , keep_alive ) ; Ok ( ( channel_packet , fut , 0 ) ) } ) . await ? ; info ! ( " " ) ; Ok ( self ) }
Connect MQTT client to the broker
Used for sending connect request to the broker after Client has been initialized.
Connect MQTT client to the broker Used for sending connect request to the broker after Client has been initialized .
&mut self
keep_alive
u16
client_id
&str
username
Option<&str>
password
Option<&[u8]>
clean_session
bool