@@ -153,88 +153,6 @@ async fn main() {
153
153
154
154
```
155
155
156
- ### Sync example:
157
- ``` rust
158
- use mqrstt :: {
159
- MqttClient ,
160
- ConnectOptions ,
161
- new_sync,
162
- packets :: {self , Packet },
163
- EventHandler ,
164
- sync :: NetworkStatus ,
165
- };
166
- use std :: net :: TcpStream ;
167
- use bytes :: Bytes ;
168
-
169
- pub struct PingPong {
170
- pub client : MqttClient ,
171
- }
172
-
173
- impl EventHandler for PingPong {
174
- // Handlers only get INCOMING packets. This can change later.
175
- fn handle (& mut self , event : packets :: Packet ) {
176
- match event {
177
- Packet :: Publish (p ) => {
178
- if let Ok (payload ) = String :: from_utf8 (p . payload. to_vec ()) {
179
- if payload . to_lowercase (). contains (" ping" ) {
180
- self . client
181
- . publish_blocking (
182
- p . topic. clone (),
183
- p . qos,
184
- p . retain,
185
- Bytes :: from_static (b " pong" ),
186
- ). unwrap ();
187
- println! (" Received Ping, Send pong!" );
188
- }
189
- }
190
- },
191
- Packet :: ConnAck (_ ) => { println! (" Connected!" ) },
192
- _ => (),
193
- }
194
- }
195
- }
196
-
197
-
198
- let mut client_id : String = " SyncTcpPingReqTestExample" . to_string ();
199
- let options = ConnectOptions :: new (client_id );
200
-
201
- let address = " broker.emqx.io" ;
202
- let port = 1883 ;
203
-
204
- let (mut network , client ) = new_sync (options );
205
-
206
- // IMPORTANT: Set nonblocking to true! No progression will be made when stream reads block!
207
- let stream = TcpStream :: connect ((address , port )). unwrap ();
208
- stream . set_nonblocking (true ). unwrap ();
209
-
210
- let mut pingpong = PingPong {
211
- client : client . clone (),
212
- };
213
-
214
- network . connect (stream , & mut pingpong ). unwrap ();
215
-
216
- let res_join_handle = std :: thread :: spawn (move ||
217
- loop {
218
- match network . poll (& mut pingpong ) {
219
- Ok (NetworkStatus :: ActivePending ) => {
220
- std :: thread :: sleep (std :: time :: Duration :: from_millis (100 ));
221
- },
222
- Ok (NetworkStatus :: ActiveReady ) => {
223
- std :: thread :: sleep (std :: time :: Duration :: from_millis (100 ));
224
- },
225
- otherwise => return otherwise ,
226
- }
227
- }
228
- );
229
-
230
- std :: thread :: sleep (std :: time :: Duration :: from_secs (30 ));
231
- client . disconnect_blocking (). unwrap ();
232
- let join_res = res_join_handle . join ();
233
- assert! (join_res . is_ok ());
234
- let res = join_res . unwrap ();
235
- assert! (res . is_ok ());
236
- ```
237
-
238
156
## FAQ
239
157
- Not much gets frequently asked, so please do! :)
240
158
- Open to feature requests
0 commit comments