Skip to content

Commit

Permalink
docs: remove sync example from readme
Browse files Browse the repository at this point in the history
  • Loading branch information
GunnarMorrigan committed Dec 3, 2024
1 parent 2dfdb4d commit b51f3b4
Showing 1 changed file with 0 additions and 82 deletions.
82 changes: 0 additions & 82 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,88 +153,6 @@ async fn main() {

```

### Sync example:
```rust
use mqrstt::{
MqttClient,
ConnectOptions,
new_sync,
packets::{self, Packet},
EventHandler,
sync::NetworkStatus,
};
use std::net::TcpStream;
use bytes::Bytes;

pub struct PingPong {
pub client: MqttClient,
}

impl EventHandler for PingPong {
// Handlers only get INCOMING packets. This can change later.
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_blocking(
p.topic.clone(),
p.qos,
p.retain,
Bytes::from_static(b"pong"),
).unwrap();
println!("Received Ping, Send pong!");
}
}
},
Packet::ConnAck(_) => { println!("Connected!") },
_ => (),
}
}
}


let mut client_id: String = "SyncTcpPingReqTestExample".to_string();
let options = ConnectOptions::new(client_id);

let address = "broker.emqx.io";
let port = 1883;

let (mut network, client) = new_sync(options);

// IMPORTANT: Set nonblocking to true! No progression will be made when stream reads block!
let stream = TcpStream::connect((address, port)).unwrap();
stream.set_nonblocking(true).unwrap();

let mut pingpong = PingPong {
client: client.clone(),
};

network.connect(stream, &mut pingpong).unwrap();

let res_join_handle = std::thread::spawn(move ||
loop {
match network.poll(&mut pingpong) {
Ok(NetworkStatus::ActivePending) => {
std::thread::sleep(std::time::Duration::from_millis(100));
},
Ok(NetworkStatus::ActiveReady) => {
std::thread::sleep(std::time::Duration::from_millis(100));
},
otherwise => return otherwise,
}
}
);

std::thread::sleep(std::time::Duration::from_secs(30));
client.disconnect_blocking().unwrap();
let join_res = res_join_handle.join();
assert!(join_res.is_ok());
let res = join_res.unwrap();
assert!(res.is_ok());
```

## FAQ
- Not much gets frequently asked, so please do! :)
- Open to feature requests
Expand Down

0 comments on commit b51f3b4

Please sign in to comment.