Skip to content

Commit 23aa593

Browse files
committed
Added more docs and examples.
1 parent 3782f0d commit 23aa593

File tree

7 files changed

+114
-3
lines changed

7 files changed

+114
-3
lines changed

src/codec/http.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! Send HTTP requests and responses asynchronously.
2+
//!
3+
//! This module has both an `HttpClientCodec` for an async HTTP client and an
4+
//! `HttpServerCodec` for an async HTTP server.
15
use std::io::{self, Write};
26
use std::error::Error;
37
use std::fmt::{self, Formatter, Display};
@@ -15,6 +19,39 @@ use bytes::BytesMut;
1519
use bytes::BufMut;
1620

1721
#[derive(Copy, Clone, Debug)]
22+
///```rust,no_run
23+
///# extern crate tokio_core;
24+
///# extern crate tokio_io;
25+
///# extern crate websocket;
26+
///# extern crate hyper;
27+
///# use websocket::codec::http::HttpClientCodec;
28+
///# use websocket::async::futures::Future;
29+
///# use websocket::async::futures::Sink;
30+
///# use tokio_core::net::TcpStream;
31+
///# use tokio_core::reactor::Core;
32+
///# use tokio_io::AsyncRead;
33+
///# use hyper::http::h1::Incoming;
34+
///# use hyper::version::HttpVersion;
35+
///# use hyper::header::Headers;
36+
///# use hyper::method::Method;
37+
///# use hyper::uri::RequestUri;
38+
///
39+
///# fn main() {
40+
///let core = Core::new().unwrap();
41+
///
42+
///let f = TcpStream::connect(&"crouton.net".parse().unwrap(), &core.handle())
43+
/// .and_then(|s| {
44+
/// Ok(s.framed(HttpClientCodec))
45+
/// })
46+
/// .and_then(|s| {
47+
/// s.send(Incoming {
48+
/// version: HttpVersion::Http11,
49+
/// subject: (Method::Get, RequestUri::AbsolutePath("/".to_string())),
50+
/// headers: Headers::new(),
51+
/// })
52+
/// });
53+
///# }
54+
///```
1855
pub struct HttpClientCodec;
1956

2057
fn split_off_http(src: &mut BytesMut) -> Option<BytesMut> {

src/codec/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
1+
//! Useful `Codec` types for asynchronously encoding and decoding messages.
2+
//!
3+
//! This is intended to be used with the `Framed` type in the `tokio-io` crate.
4+
//! This module contains an `http` codec which can be used to send and receive
5+
//! hyper HTTP requests and responses asynchronously.
6+
//! See it's module level documentation for more info.
7+
//!
8+
//! Second but most importantly this module contains a codec for asynchronously
9+
//! encoding and decoding websocket messages (and dataframes if you want to go
10+
//! more low level) in the `ws` module.
11+
//! See it's module level documentation for more info.
12+
113
pub mod http;
214
pub mod ws;

src/dataframe.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ impl DataFrame {
3737
}
3838
}
3939

40-
/// TODO: docs
40+
/// Take the body and header of a dataframe and combine it into a single
41+
/// Dataframe struct. A websocket message can be made up of many individual
42+
/// dataframes, use the methods from the Message or OwnedMessage structs to
43+
/// take many of these and create a websocket message.
4144
pub fn read_dataframe_body(
4245
header: DataFrameHeader,
4346
body: Vec<u8>,

src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ extern crate tokio_io;
5252
#[cfg(feature="async")]
5353
extern crate bytes;
5454
#[cfg(feature="async")]
55-
extern crate futures;
55+
pub extern crate futures;
5656
#[cfg(feature="async-ssl")]
5757
extern crate tokio_tls;
5858

@@ -147,6 +147,12 @@ pub mod async {
147147
pub use client::async::Client;
148148

149149
pub use result::async::WebSocketFuture;
150+
151+
pub use futures;
152+
pub use tokio_core::net::TcpStream;
153+
pub use tokio_core::net::TcpListener;
154+
pub use tokio_core::reactor::Core;
155+
pub use tokio_core::reactor::Handle;
150156
}
151157

152158
pub use self::message::Message;

src/message.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,14 @@ impl<'a> ws::Message for Message<'a> {
229229
}
230230
}
231231

232-
/// Represents a WebSocket message.
232+
/// Represents an owned WebSocket message.
233+
///
234+
/// `OwnedMessage`s are generated when the user receives a message (since the data
235+
/// has to be copied out of the network buffer anyway).
236+
/// If you would like to create a message out of borrowed data to use for sending
237+
/// please use the `Message` struct (which contains a `Cow`).
238+
///
239+
/// Note that `OwnedMessage` can `Message` can be converted into each other.
233240
#[derive(Eq, PartialEq, Clone, Debug)]
234241
pub enum OwnedMessage {
235242
/// A message containing UTF-8 text data
@@ -249,13 +256,28 @@ pub enum OwnedMessage {
249256
}
250257

251258
impl OwnedMessage {
259+
/// Checks if this message is a close message.
260+
///
261+
///```rust
262+
///# use websocket::OwnedMessage;
263+
///assert!(OwnedMessage::Close(None).is_close());
264+
///```
252265
pub fn is_close(&self) -> bool {
253266
match *self {
254267
OwnedMessage::Close(_) => true,
255268
_ => false,
256269
}
257270
}
258271

272+
/// Checks if this message is a control message.
273+
/// Control messages are either `Close`, `Ping`, or `Pong`.
274+
///
275+
///```rust
276+
///# use websocket::OwnedMessage;
277+
///assert!(OwnedMessage::Ping(vec![]).is_control());
278+
///assert!(OwnedMessage::Pong(vec![]).is_control());
279+
///assert!(OwnedMessage::Close(None).is_control());
280+
///```
259281
pub fn is_control(&self) -> bool {
260282
match *self {
261283
OwnedMessage::Close(_) => true,
@@ -265,17 +287,40 @@ impl OwnedMessage {
265287
}
266288
}
267289

290+
/// Checks if this message is a data message.
291+
/// Data messages are either `Text` or `Binary`.
292+
///
293+
///```rust
294+
///# use websocket::OwnedMessage;
295+
///assert!(OwnedMessage::Text("1337".to_string()).is_data());
296+
///assert!(OwnedMessage::Binary(vec![]).is_data());
297+
///```
268298
pub fn is_data(&self) -> bool {
269299
!self.is_control()
270300
}
271301

302+
/// Checks if this message is a ping message.
303+
/// `Ping` messages can come at any time and usually generate a `Pong` message
304+
/// response.
305+
///
306+
///```rust
307+
///# use websocket::OwnedMessage;
308+
///assert!(OwnedMessage::Ping("ping".to_string().into_bytes()).is_ping());
309+
///```
272310
pub fn is_ping(&self) -> bool {
273311
match *self {
274312
OwnedMessage::Ping(_) => true,
275313
_ => false,
276314
}
277315
}
278316

317+
/// Checks if this message is a pong message.
318+
/// `Pong` messages are usually sent only in response to `Ping` messages.
319+
///
320+
///```rust
321+
///# use websocket::OwnedMessage;
322+
///assert!(OwnedMessage::Pong("pong".to_string().into_bytes()).is_pong());
323+
///```
279324
pub fn is_pong(&self) -> bool {
280325
match *self {
281326
OwnedMessage::Pong(_) => true,

src/result.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ use native_tls::HandshakeError as TlsHandshakeError;
1717
/// The type used for WebSocket results
1818
pub type WebSocketResult<T> = Result<T, WebSocketError>;
1919

20+
/// This module contains convenience types to make working with Futures and
21+
/// websocket results easier.
2022
#[cfg(feature="async")]
2123
pub mod async {
2224
use futures::Future;
2325
use super::WebSocketError;
26+
27+
/// The most common Future in this library, it is simply some result `I` or
28+
/// a `WebSocketError`. This is analogous to the `WebSocketResult` type.
2429
pub type WebSocketFuture<I> = Box<Future<Item = I, Error = WebSocketError>>;
2530
}
2631

src/ws/sender.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ use result::WebSocketResult;
99

1010
/// A trait for sending data frames and messages.
1111
pub trait Sender {
12+
/// Should the messages sent be masked.
13+
/// See the [RFC](https://tools.ietf.org/html/rfc6455#section-5.3)
14+
/// for more detail.
1215
fn is_masked(&self) -> bool;
1316

1417
/// Sends a single data frame using this sender.

0 commit comments

Comments
 (0)