-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5caa701
commit 3439e95
Showing
7 changed files
with
179 additions
and
101 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use mqrstt::{ | ||
packets::{self, Packet}, | ||
AsyncEventHandler, MqttClient, NetworkBuilder, NetworkStatus, | ||
}; | ||
use tokio::time::Duration; | ||
|
||
pub struct PingPong { | ||
pub client: MqttClient, | ||
} | ||
impl AsyncEventHandler for PingPong { | ||
// Handlers only get INCOMING packets. | ||
async fn handle(&mut self, event: packets::Packet) { | ||
match event { | ||
Packet::Publish(p) => { | ||
if let Ok(payload) = String::from_utf8(p.payload.to_vec()) { | ||
if payload.to_lowercase().contains("ping") { | ||
self.client.publish(p.topic.clone(), p.qos, p.retain, b"pong").await.unwrap(); | ||
println!("Received Ping, Send pong!"); | ||
} | ||
} | ||
} | ||
Packet::ConnAck(_) => { | ||
println!("Connected!") | ||
} | ||
_ => (), | ||
} | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let (mut network, client) = NetworkBuilder::new_from_client_id("TokioTcpPingPongExample").tokio_network(); | ||
|
||
let stream = tokio::net::TcpStream::connect(("broker.emqx.io", 1883)).await.unwrap(); | ||
let stream = tokio::io::BufStream::new(stream); | ||
|
||
let mut pingpong = PingPong { client: client.clone() }; | ||
|
||
network.connect(stream, &mut pingpong).await.unwrap(); | ||
|
||
client.subscribe("mqrstt").await.unwrap(); | ||
|
||
let network_handle = tokio::spawn(async move { | ||
let result = network.run(&mut pingpong).await; | ||
(result, pingpong) | ||
}); | ||
|
||
tokio::time::sleep(Duration::from_secs(30)).await; | ||
client.disconnect().await.unwrap(); | ||
|
||
let (result, _pingpong) = network_handle.await.unwrap(); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), NetworkStatus::OutgoingDisconnect); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use mqrstt::{ | ||
packets::{self, Packet}, | ||
AsyncEventHandler, ConnectOptions, MqttClient, NetworkBuilder, NetworkStatus, | ||
}; | ||
pub struct PingPong { | ||
pub client: MqttClient, | ||
} | ||
impl AsyncEventHandler for PingPong { | ||
// Handlers only get INCOMING packets. This can change later. | ||
async fn handle(&mut self, event: packets::Packet) { | ||
match event { | ||
Packet::Publish(p) => { | ||
if let Ok(payload) = String::from_utf8(p.payload.to_vec()) { | ||
if payload.to_lowercase().contains("ping") { | ||
self.client.publish(p.topic.clone(), p.qos, p.retain, b"pong").await.unwrap(); | ||
println!("Received Ping, Send pong!"); | ||
} | ||
} | ||
} | ||
Packet::ConnAck(_) => { | ||
println!("Connected!") | ||
} | ||
_ => (), | ||
} | ||
} | ||
} | ||
fn main() { | ||
smol::block_on(async { | ||
let (mut network, client) = NetworkBuilder::new_from_client_id("mqrsttSmolExample").smol_network(); | ||
let stream = smol::net::TcpStream::connect(("broker.emqx.io", 1883)).await.unwrap(); | ||
|
||
let mut pingpong = PingPong { client: client.clone() }; | ||
|
||
network.connect(stream, &mut pingpong).await.unwrap(); | ||
|
||
// This subscribe is only processed when we run the network | ||
client.subscribe("mqrstt").await.unwrap(); | ||
|
||
let task_handle = smol::spawn(async move { | ||
let result = network.run(&mut pingpong).await; | ||
(result, pingpong) | ||
}); | ||
|
||
smol::Timer::after(std::time::Duration::from_secs(30)).await; | ||
client.disconnect().await.unwrap(); | ||
|
||
let (result, _pingpong) = task_handle.await; | ||
|
||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), NetworkStatus::OutgoingDisconnect); | ||
}); | ||
} |
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
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
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
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