pub struct WebSocketUpgrade<F = DefaultOnFailedUpgrade> { /* private fields */ }fullstack only.Expand description
Extractor for establishing WebSocket connections.
For HTTP/1.1 requests, this extractor requires the request method to be GET;
in later versions, CONNECT is used instead.
To support both, it should be used with any.
See the module docs for an example.
Implementations§
Source§impl<F> WebSocketUpgrade<F>
impl<F> WebSocketUpgrade<F>
Sourcepub fn read_buffer_size(self, size: usize) -> WebSocketUpgrade<F>
Available on crate feature ws only.
pub fn read_buffer_size(self, size: usize) -> WebSocketUpgrade<F>
ws only.Read buffer capacity. The default value is 128KiB
Sourcepub fn write_buffer_size(self, size: usize) -> WebSocketUpgrade<F>
Available on crate feature ws only.
pub fn write_buffer_size(self, size: usize) -> WebSocketUpgrade<F>
ws only.The target minimum size of the write buffer to reach before writing the data to the underlying stream.
The default value is 128 KiB.
If set to 0 each message will be eagerly written to the underlying stream.
It is often more optimal to allow them to buffer a little, hence the default value.
Note: flush will always fully write the buffer regardless.
Sourcepub fn max_write_buffer_size(self, max: usize) -> WebSocketUpgrade<F>
Available on crate feature ws only.
pub fn max_write_buffer_size(self, max: usize) -> WebSocketUpgrade<F>
ws only.The max size of the write buffer in bytes. Setting this can provide backpressure in the case the write buffer is filling up due to write errors.
The default value is unlimited.
Note: The write buffer only builds up past write_buffer_size
when writes to the underlying stream are failing. So the write buffer can not
fill up if you are not observing write errors even if not flushing.
Note: Should always be at least write_buffer_size + 1 message
and probably a little more depending on error handling strategy.
Sourcepub fn max_message_size(self, max: usize) -> WebSocketUpgrade<F>
Available on crate feature ws only.
pub fn max_message_size(self, max: usize) -> WebSocketUpgrade<F>
ws only.Set the maximum message size (defaults to 64 megabytes)
Sourcepub fn max_frame_size(self, max: usize) -> WebSocketUpgrade<F>
Available on crate feature ws only.
pub fn max_frame_size(self, max: usize) -> WebSocketUpgrade<F>
ws only.Set the maximum frame size (defaults to 16 megabytes)
Sourcepub fn accept_unmasked_frames(self, accept: bool) -> WebSocketUpgrade<F>
Available on crate feature ws only.
pub fn accept_unmasked_frames(self, accept: bool) -> WebSocketUpgrade<F>
ws only.Allow server to accept unmasked frames (defaults to false)
Sourcepub fn protocols<I>(self, protocols: I) -> WebSocketUpgrade<F>
Available on crate feature ws only.
pub fn protocols<I>(self, protocols: I) -> WebSocketUpgrade<F>
ws only.Set the known protocols.
If the protocol name specified by Sec-WebSocket-Protocol header
to match any of them, the upgrade response will include Sec-WebSocket-Protocol header and
return the protocol name.
The protocols should be listed in decreasing order of preference: if the client offers multiple protocols that the server could support, the server will pick the first one in this list.
§Examples
use axum::{
extract::ws::{WebSocketUpgrade, WebSocket},
routing::any,
response::{IntoResponse, Response},
Router,
};
let app = Router::new().route("/ws", any(handler));
async fn handler(ws: WebSocketUpgrade) -> Response {
ws.protocols(["graphql-ws", "graphql-transport-ws"])
.on_upgrade(|socket| async {
// ...
})
}Sourcepub fn requested_protocols(&self) -> impl Iterator<Item = &HeaderValue>
Available on crate feature ws only.
pub fn requested_protocols(&self) -> impl Iterator<Item = &HeaderValue>
ws only.Return the WebSocket subprotocols requested by the client.
§Examples
If the client sends the following HTTP header in the WebSocket upgrade request:
Sec-WebSocket-Protocol: soap, wampthis method returns an iterator yielding "soap" and "wamp".
Sourcepub fn set_selected_protocol(&mut self, protocol: HeaderValue)
Available on crate feature ws only.
pub fn set_selected_protocol(&mut self, protocol: HeaderValue)
ws only.Set the chosen WebSocket subprotocol.
Another method, protocols(), also sets the chosen WebSocket
subprotocol. If both methods are called, only the latter call takes effect.
§Notes
- The chosen protocol is echoed back in the WebSocket upgrade response as required by RFC 6455. Some browsers may reject a value that was not present in the client’s request.
Sourcepub fn selected_protocol(&self) -> Option<&HeaderValue>
Available on crate feature ws only.
pub fn selected_protocol(&self) -> Option<&HeaderValue>
ws only.Return the selected WebSocket subprotocol, if one has been chosen.
If protocols() selects a matching protocol, or
set_selected_protocol() has been called, the return
value will be Some containing the selected protocol. Otherwise, it will be None.
Sourcepub fn on_failed_upgrade<C>(self, callback: C) -> WebSocketUpgrade<C>where
C: OnFailedUpgrade,
Available on crate feature ws only.
pub fn on_failed_upgrade<C>(self, callback: C) -> WebSocketUpgrade<C>where
C: OnFailedUpgrade,
ws only.Provide a callback to call if upgrading the connection fails.
The connection upgrade is performed in a background task. If that fails this callback will be called.
By default any errors will be silently ignored.
§Example
use axum::{
extract::{WebSocketUpgrade},
response::Response,
};
async fn handler(ws: WebSocketUpgrade) -> Response {
ws.on_failed_upgrade(|error| {
report_error(error);
})
.on_upgrade(|socket| async { /* ... */ })
}