Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional Documentation of IntoClientRequest on connect_async #342

Merged
merged 4 commits into from
Aug 8, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,30 @@ use tungstenite::{
use crate::{domain, stream::MaybeTlsStream, Connector, IntoClientRequest, WebSocketStream};

/// Connect to a given URL.
///
/// Accepts any request that implements [`IntoClientRequest`], which is often just `&str`, but can
/// be a variety of types such as `httparse::Request` or [`tungstenite::http::Request`] for more
/// complex uses.
///
/// ```no_run
/// # async fn example() {
/// use tungstenite::http::{Method, Request};
/// use tokio_tungstenite::connect_async;
///
/// let request = Request::builder()
/// .uri("wss://api.example.com")
/// .method(Method::GET)
/// .header("Sec-WebSocket-Key", "someUniqueValue")
/// .header("Sec-WebSocket-Version", "13")
/// .header("host", "api.example.com")
/// .header("Connection", "Upgrade")
/// .header("Upgrade", "websocket")
/// .body(())
/// .unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this kind of pattern is actually a bit problematic as it requires specifying all WebSocket headers properly. A more typical issue that people experience is that they just want to add one or more additional headers on top of what we generate automatically, see snapview/tungstenite-rs#327

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's even better for my use case, too. I've updated the example if you're okay with adding it. Thanks!

///
/// let (stream, response) = connect_async(request).await.unwrap();
/// #}
/// ```
pub async fn connect_async<R>(
request: R,
) -> Result<(WebSocketStream<MaybeTlsStream<TcpStream>>, Response), Error>
Expand Down
Loading