Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions examples/http2_websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
//! ```

use futures_util::{SinkExt, StreamExt, TryStreamExt};
use wreq::{header, ws::message::Message};
use wreq::{Version, header, ws::message::Message};

#[tokio::main]
async fn main() -> wreq::Result<()> {
// Use the API you're already familiar with
let resp = wreq::websocket("wss://127.0.0.1:3000/ws")
.force_http2()
.version(Version::HTTP_2)
.header(header::USER_AGENT, env!("CARGO_PKG_NAME"))
.read_buffer_size(1024 * 1024)
.send()
Expand Down
27 changes: 17 additions & 10 deletions src/client/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,27 @@ impl WebSocketRequestBuilder {
self
}

/// Forces the WebSocket connection to use HTTP/2 protocol.
/// Set HTTP version
///
/// This method configures the WebSocket connection to use HTTP/2's Extended
/// CONNECT Protocol (RFC 8441) for the handshake instead of the traditional
/// HTTP/1.1 upgrade mechanism.
/// Configures the HTTP version used for the WebSocket handshake.
/// Defaults to HTTP/1.1.
///
/// # Behavior
/// # HTTP/1.1 (default)
///
/// - Uses `CONNECT` method with `:protocol: websocket` pseudo-header
/// - Requires server support for HTTP/2 WebSocket connections
/// - Will fail if server doesn't support HTTP/2 WebSocket upgrade
/// - Uses the standard `Upgrade: websocket` mechanism (RFC 6455)
/// - Sends an HTTP `GET` request with `Connection: Upgrade` and `Upgrade: websocket` headers
/// - Widely supported by servers
///
/// # HTTP/2
///
/// - Uses the Extended CONNECT Protocol (RFC 8441)
/// - Sends a `CONNECT` request with the `:protocol: websocket` pseudo-header instead of the
/// traditional upgrade mechanism
/// - Requires explicit server support for HTTP/2 WebSocket connections
/// - Will fail if the server does not support HTTP/2 WebSocket upgrade
#[inline]
pub fn force_http2(mut self) -> Self {
self.inner = self.inner.version(Version::HTTP_2);
pub fn version(mut self, version: Version) -> Self {
self.inner = self.inner.version(version);
self
}
Comment thread
0x676e67 marked this conversation as resolved.

Expand Down
Loading