Struct axum::extract::WebSocketUpgrade
source · pub struct WebSocketUpgrade { /* private fields */ }
ws
only.Expand description
Extractor for establishing WebSocket connections.
Note: This extractor requires the request method to be GET
so it should
always be used with get
. Requests with other methods will be
rejected.
See the module docs for an example.
Implementations
sourceimpl WebSocketUpgrade
impl WebSocketUpgrade
sourcepub fn max_send_queue(self, max: usize) -> Self
pub fn max_send_queue(self, max: usize) -> Self
Set the size of the internal message send queue.
sourcepub fn max_message_size(self, max: usize) -> Self
pub fn max_message_size(self, max: usize) -> Self
Set the maximum message size (defaults to 64 megabytes)
sourcepub fn max_frame_size(self, max: usize) -> Self
pub fn max_frame_size(self, max: usize) -> Self
Set the maximum frame size (defaults to 16 megabytes)
sourcepub fn protocols<I>(self, protocols: I) -> Selfwhere
I: IntoIterator,
I::Item: Into<Cow<'static, str>>,
pub fn protocols<I>(self, protocols: I) -> Selfwhere
I: IntoIterator,
I::Item: Into<Cow<'static, str>>,
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::get,
response::{IntoResponse, Response},
Router,
};
let app = Router::new().route("/ws", get(handler));
async fn handler(ws: WebSocketUpgrade) -> Response {
ws.protocols(["graphql-ws", "graphql-transport-ws"])
.on_upgrade(|socket| async {
// ...
})
}
sourcepub fn on_upgrade<F, Fut>(self, callback: F) -> Responsewhere
F: FnOnce(WebSocket) -> Fut + Send + 'static,
Fut: Future<Output = ()> + Send + 'static,
pub fn on_upgrade<F, Fut>(self, callback: F) -> Responsewhere
F: FnOnce(WebSocket) -> Fut + Send + 'static,
Fut: Future<Output = ()> + Send + 'static,
Finalize upgrading the connection and call the provided callback with the stream.
When using WebSocketUpgrade
, the response produced by this method
should be returned from the handler. See the module docs for an
example.