1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//! `zerocopy`, `async`, `no_std` and [`autobahn`](https://github.com/crossbario/autobahn-testsuite) compliant `websockets` implementation.
//!
//! # Traits
//!
//! This library is based on [`embedded_io_async`](https://docs.rs/embedded-io-async/0.7/embedded_io_async/)'s
//! [`Read`](https://docs.rs/embedded-io-async/0.7/embedded_io_async/trait.Read.html) and [`Write`](https://docs.rs/embedded-io-async/0.7/embedded_io_async/trait.Write.html) and [`rand_core`](https://docs.rs/rand_core/0.10/rand_core/)'s [`Rng`](https://docs.rs/rand_core/0.10/rand_core/trait.Rng.html) traits.
//!
//! It's recommended to use [`embedded_io_adapters`](https://docs.rs/embedded-io-adapters/0.7/embedded_io_adapters/) if you are using other async `Read` and `Write` traits like [`tokio`](https://docs.rs/tokio/1.0/tokio/index.html)'s [`AsyncRead`](https://docs.rs/tokio/1.0/tokio/io/trait.AsyncRead.html) and [`AsyncWrite`](https://docs.rs/tokio/1.0/tokio/io/trait.AsyncWrite.html).
//!
//! See the examples folder for more information.
//!
//! # Examples
//!
//! In the following examples, `Noop` is a mock type that implements the required traits for using a [`WebSocket`].
//! - A `stream` is anything that implements [`embedded_io_async::Read`] + [`embedded_io_async::Write`].
//! - An `rng` is anything that implements [`rand_core::Rng`].
//!
//! ### Client
//! ```
//! # async fn client() {
//! # use websocketz::mock::Noop;
//! use websocketz::{Message, WebSocket, http::Header, next, options::ConnectOptions};
//!
//! // An already connected stream.
//! // Impl embedded_io_async Read + Write.
//! let stream = Noop;
//!
//! let read_buffer = &mut [0u8; 1024];
//! let write_buffer = &mut [0u8; 1024];
//! let fragments_buffer = &mut [0u8; 1024];
//!
//! // Impl rand_core RngCore.
//! let rng = Noop;
//!
//! // Perform a WebSocket handshake as a client.
//! // 16 is the max number of headers to allocate space for.
//! let mut websocketz = WebSocket::connect::<16>(
//! // Set the connection options.
//! // The path for the WebSocket endpoint as well as any additional HTTP headers.
//! ConnectOptions::default()
//! .with_path("/ws")
//! .expect("Valid path")
//! .with_headers(&[
//! Header {
//! name: "Host",
//! value: b"example.com",
//! },
//! Header {
//! name: "User-Agent",
//! value: b"WebSocketz",
//! },
//! ]),
//! stream,
//! rng,
//! read_buffer,
//! write_buffer,
//! fragments_buffer,
//! )
//! .await
//! .expect("Handshake failed");
//!
//! // Send a text message.
//! websocketz
//! .send(Message::Text("Hello, WebSocket!"))
//! .await
//! .expect("Failed to send message");
//!
//! // Receive messages in a loop.
//! loop {
//! match next!(websocketz) {
//! None => {
//! // Connection closed.
//! break;
//! }
//! Some(Ok(msg)) => {
//! // Handle received message.
//! let _ = msg;
//! }
//! Some(Err(err)) => {
//! // Handle error.
//! let _ = err;
//!
//! break;
//! }
//! }
//! }
//! # }
//! ```
//!
//! ### Server
//! ```
//! # async fn server() {
//! # use websocketz::mock::Noop;
//! use websocketz::{Message, WebSocket, http::Header, next, options::AcceptOptions};
//!
//! // An already connected stream.
//! // Impl embedded_io_async Read + Write.
//! let stream = Noop;
//!
//! let read_buffer = &mut [0u8; 1024];
//! let write_buffer = &mut [0u8; 1024];
//! let fragments_buffer = &mut [0u8; 1024];
//!
//! // Impl rand_core RngCore.
//! let rng: Noop = Noop;
//!
//! // Perform a WebSocket handshake as a server.
//! // 16 is the max number of headers to allocate space for.
//! let mut websocketz = WebSocket::accept::<16>(
//! // Set the acceptance options.
//! // Any additional HTTP headers.
//! AcceptOptions::default().with_headers(&[Header {
//! name: "Server",
//! value: b"WebSocketz",
//! }]),
//! stream,
//! rng,
//! read_buffer,
//! write_buffer,
//! fragments_buffer,
//! )
//! .await
//! .expect("Handshake failed");
//!
//! // Receive messages in a loop.
//! loop {
//! match next!(websocketz) {
//! None => {
//! // Connection closed.
//! break;
//! }
//! Some(Ok(msg)) => {
//! // Handle received message.
//! let _ = msg;
//!
//! // Send a binary message.
//! if let Err(err) = websocketz.send(Message::Binary(b"Hello, WebSocket!")).await {
//! let _ = err;
//!
//! break;
//! }
//! }
//! Some(Err(err)) => {
//! // Handle error.
//! let _ = err;
//!
//! break;
//! }
//! }
//! }
//! # }
//! ```
//!
//! # Laziness
//!
//! This library is `lazy`, meaning that the WebSocket connection is managed as long as you read from the connection.
//!
//! Managing the connection consists of two parts:
//! - Sending [Message::Pong] messages in response to [Message::Ping] messages.
//! - Responding to [Message::Close] messages by sending the appropriate [Message::Close] response and closing the connection.
//!
//! `auto_pong` and `auto_close` are enabled by default, but can be set using [`WebSocket::with_auto_pong`] and [`WebSocket::with_auto_close`] respectively.
//!
//! # Reading from the connection
//!
//! This library allocates nothing. It only uses exclusive references and stack memory. It is quite challenging to offer a clean API while adhering to rust's borrowing rules.
//! That's why a [`WebSocket`] does not offer any method to read messages directly.
//!
//! Instead, you can use the [`next!`] macro to read messages from the connection.
//!
//! [`next!`] unpacks the internal `private` structure of the [`WebSocket`] to obtain mutable references and perform reads.
//!
//! ```
//! # async fn next_macro() {
//! # use websocketz::mock::Noop;
//! # use websocketz::{WebSocket, next, options::ConnectOptions};
//! #
//! # let stream = Noop;
//! # let read_buffer = &mut [0u8; 1024];
//! # let write_buffer = &mut [0u8; 1024];
//! # let fragments_buffer = &mut [0u8; 1024];
//! # let rng = Noop;
//! #
//! # let websocketz = WebSocket::connect::<16>(
//! # ConnectOptions::default()
//! # .with_path("/ws")
//! # .expect("Valid path"),
//! # stream,
//! # rng,
//! # read_buffer,
//! # write_buffer,
//! # fragments_buffer,
//! # )
//! # .await
//! # .expect("Handshake failed");
//! #
//! # let existing_websocket = || websocketz;
//! let mut websocketz = existing_websocket();
//!
//! while let Some(Ok(msg)) = next!(websocketz) {
//! // Messages hold references to the websocket buffers.
//! let _ = msg;
//! }
//! # }
//! ```
//!
//! # Writing to the connection
//!
//! [`WebSocket`] offers two methods to send messages, [`WebSocket::send`] and [`WebSocket::send_fragmented`].
//! These methods take `&mut self`, which might be problematic in some situations. E.g., echoing back a received message.
//! ```compile_fail
//! # async fn send_method_no_compile() {
//! # use crate::mock::Noop;
//! # use crate::{WebSocket, next, options::ConnectOptions};
//! #
//! # let stream = Noop;
//! # let read_buffer = &mut [0u8; 1024];
//! # let write_buffer = &mut [0u8; 1024];
//! # let fragments_buffer = &mut [0u8; 1024];
//! # let rng = Noop;
//! #
//! # let websocketz = WebSocket::connect::<16>(
//! # ConnectOptions::default()
//! # .with_path("/ws")
//! # .expect("Valid path"),
//! # stream,
//! # rng,
//! # read_buffer,
//! # write_buffer,
//! # fragments_buffer,
//! # )
//! # .await
//! # .expect("Handshake failed");
//! #
//! # let existing_websocket = || websocketz;
//! let mut websocketz = existing_websocket();
//!
//! while let Some(Ok(msg)) = next!(websocketz) {
//! // Messages hold references to the websocket buffers.
//! // So this will not compile:
//! // cannot borrow `websocketz` as mutable more than once at a time.
//! websocketz.send(msg).await.expect("Failed to send message");
//! }
//! # }
//! ```
//!
//! To work around this limitation, the library offers the [`send!`] and [`send_fragmented!`] macros, which work similarly to the [`next!`] macro by unpacking the internal `private` structure of the [`WebSocket`].
//!
//! ```
//! # async fn send_macro() {
//! # use websocketz::mock::Noop;
//! # use websocketz::{WebSocket, next, options::ConnectOptions, send};
//! #
//! # let stream = Noop;
//! # let read_buffer = &mut [0u8; 1024];
//! # let write_buffer = &mut [0u8; 1024];
//! # let fragments_buffer = &mut [0u8; 1024];
//! # let rng = Noop;
//! #
//! # let websocketz = WebSocket::connect::<16>(
//! # ConnectOptions::default()
//! # .with_path("/ws")
//! # .expect("Valid path"),
//! # stream,
//! # rng,
//! # read_buffer,
//! # write_buffer,
//! # fragments_buffer,
//! # )
//! # .await
//! # .expect("Handshake failed");
//! #
//! # let existing_websocket = || websocketz;
//! let mut websocketz = existing_websocket();
//!
//! while let Some(Ok(msg)) = next!(websocketz) {
//! send!(websocketz, msg).expect("Failed to send message");
//! }
//! # }
//!```
//!
//! # Splitting the connection
//!
//! In some cases, you might want to split the WebSocket connection into a read half and a write half.
//! This can be achieved using the [`WebSocket::split_with`] method, which returns a [`WebSocketRead`] and [`WebSocketWrite`] tuple.
//!
//! <div class="warning">
//! Due to the `lazy` nature of the library, splitting the connection will sacrifice the automatic handling of `Ping` and `Close` messages.
//! </div>
//!
//! ```
//! # async fn split() {
//! # use websocketz::mock::Noop;
//! # use websocketz::{Message, WebSocket, next, options::ConnectOptions};
//! #
//! # let stream = Noop;
//! # let read_buffer = &mut [0u8; 1024];
//! # let write_buffer = &mut [0u8; 1024];
//! # let fragments_buffer = &mut [0u8; 1024];
//! # let rng = Noop;
//! #
//! # let websocketz = WebSocket::connect::<16>(
//! # ConnectOptions::default()
//! # .with_path("/ws")
//! # .expect("Valid path"),
//! # stream,
//! # rng,
//! # read_buffer,
//! # write_buffer,
//! # fragments_buffer,
//! # )
//! # .await
//! # .expect("Handshake failed");
//! #
//! # let existing_websocket = || websocketz;
//! fn split(stream: Noop) -> (Noop, Noop) {
//! // Let's assume we split the stream here.
//!
//! (Noop, Noop)
//! }
//!
//! let websocketz = existing_websocket();
//!
//! let (mut websocketz_read, mut websocketz_write) = websocketz.split_with(split);
//!
//! websocketz_write
//! .send(Message::Text("Hello, WebSocket!"))
//! .await
//! .expect("Failed to send message");
//!
//! while let Some(Ok(msg)) = next!(websocketz_read) {
//! // `send` method works here,
//! // because `websocketz_read` and `websocketz_write` do not hold the same references.
//! websocketz_write
//! .send(msg)
//! .await
//! .expect("Failed to send message");
//! }
//! # }
//!```
pub use CloseCode;
pub use CloseFrame;
use FramesCodec;
use ;
pub use Message;
use OpCode;
use ;
pub use ;
extern crate std;