Created
July 7, 2024 11:12
-
-
Save flyaruu/71f9448474be28f0c9050fdcc86cbba8 to your computer and use it in GitHub Desktop.
MQTT embedded-tls issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "test-mqtt-std" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] | |
embedded-tls = { git="https://github.com/drogue-iot/embedded-tls"} | |
rand = "0.8.5" | |
tokio = { version = "1.38.0", features = ["macros", "net", "rt", "rt-multi-thread", "tokio-macros", "time"] } | |
embedded-io-adapters = { version = "0.6", features = ["tokio-1"] } | |
embedded-io-async = "0.6" | |
log = { version = "0.4.22", features = ["std"] } | |
env_logger = "0.11.3" | |
rust-mqtt = { version = "0.3.0", features = ["tls"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::{net::ToSocketAddrs, time::Duration}; | |
use embedded_io_adapters::tokio_1::FromTokio; | |
use embedded_tls::{Aes256GcmSha384, TlsConfig, TlsConnection, TlsContext, UnsecureProvider}; | |
use log::{error, info}; | |
use rand:: | |
rngs::OsRng | |
; | |
use rust_mqtt::{client::{client::MqttClient, client_config::{ClientConfig, MqttVersion}}, packet::v5::publish_packet::QualityOfService::{self}, utils::rng_generator::CountingRng}; | |
use tokio::{net::TcpSocket, time::sleep}; | |
#[tokio::main] | |
async fn main() { | |
env_logger::init(); | |
let host = "mqtt.eclipseprojects.io"; | |
let socket = TcpSocket::new_v4().unwrap(); | |
let mqtt_addr = (host, 8883_u16) | |
.to_socket_addrs() | |
.expect("Unable to resolve the IP address") | |
.next() | |
.expect("DNS resolution returned no IP addresses"); | |
let tcp_connection = socket.connect(mqtt_addr).await.unwrap(); | |
let config: TlsConfig<'_> = TlsConfig::new().with_server_name(&host).enable_rsa_signatures(); | |
let mut read_record_buffer = [0; 16640]; | |
let mut write_record_buffer = [0; 16640]; | |
let mut tls_connection: TlsConnection<_, _> = TlsConnection::new( | |
FromTokio::new(tcp_connection), | |
&mut read_record_buffer, | |
&mut write_record_buffer, | |
); | |
tls_connection.open(TlsContext::new( | |
&config, | |
UnsecureProvider::new::<Aes256GcmSha384>(OsRng), | |
)).await.unwrap(); | |
let mut config = ClientConfig::new(MqttVersion::MQTTv5, CountingRng(20000)); | |
config.max_packet_size = 100; | |
let mut recv_buffer = [0; 80]; | |
let mut write_buffer = [0; 80]; | |
let mut client = MqttClient::<_, 5, CountingRng>::new( | |
tls_connection, | |
&mut write_buffer, | |
80, | |
&mut recv_buffer, | |
80, | |
config, | |
); | |
client.connect_to_broker().await.unwrap(); | |
sleep(Duration::from_secs(5)).await; | |
info!("Cleint connected!"); | |
let mut count = 0; | |
loop { | |
let msg = format!("Message # {count}"); | |
let topic = "frank_lee"; | |
match client.send_message(topic, msg.as_bytes(), QualityOfService::QoS0, false).await { | |
Ok(_) => info!("yay"), | |
Err(e) => { | |
error!("Error sending message: {:?}",e); | |
}, | |
} | |
info!("Message sent to topic {} body: {}",topic,msg); | |
sleep(Duration::from_secs(5)).await; | |
count+=1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment