From c54d73e0bbf517284ef2d0092e0e77746db2b60e Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Mon, 7 Sep 2020 14:04:54 +0300 Subject: [PATCH] Improve awc websocket docs (#1654) Co-authored-by: Rob Ede --- awc/src/lib.rs | 3 ++- awc/src/ws.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/awc/src/lib.rs b/awc/src/lib.rs index 1cc31a194e4..a98d6767d06 100644 --- a/awc/src/lib.rs +++ b/awc/src/lib.rs @@ -193,7 +193,8 @@ impl Client { self.request(Method::OPTIONS, url) } - /// Construct WebSockets request. + /// Initialize a WebSocket connection. + /// Returns a WebSocket connection builder. pub fn ws(&self, url: U) -> ws::WebsocketsRequest where Uri: TryFrom, diff --git a/awc/src/ws.rs b/awc/src/ws.rs index 8b01e571650..96687ac7489 100644 --- a/awc/src/ws.rs +++ b/awc/src/ws.rs @@ -1,4 +1,31 @@ //! Websockets client +//! +//! Type definitions required to use [`awc::Client`](../struct.Client.html) as a WebSocket client. +//! +//! # Example +//! +//! ``` +//! use awc::{Client, ws}; +//! use futures_util::{sink::SinkExt, stream::StreamExt}; +//! +//! #[actix_rt::main] +//! async fn main() { +//! let (_resp, mut connection) = Client::new() +//! .ws("ws://echo.websocket.org") +//! .connect() +//! .await +//! .unwrap(); +//! +//! connection +//! .send(ws::Message::Text("Echo".to_string())) +//! .await +//! .unwrap(); +//! let response = connection.next().await.unwrap().unwrap(); +//! +//! assert_eq!(response, ws::Frame::Text("Echo".as_bytes().into())); +//! } +//! ``` + use std::convert::TryFrom; use std::net::SocketAddr; use std::rc::Rc;