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) -> Self where
I: IntoIterator,
I::Item: Into<Cow<'static, str>>,
pub fn protocols<I>(self, protocols: I) -> Self where
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,
Router,
};
let app = Router::new().route("/ws", get(handler));
async fn handler(ws: WebSocketUpgrade) -> impl IntoResponse {
ws.protocols(["graphql-ws", "graphql-transport-ws"])
.on_upgrade(|socket| async {
// ...
})
}sourcepub fn on_upgrade<F, Fut>(self, callback: F) -> Response where
F: FnOnce(WebSocket) -> Fut + Send + 'static,
Fut: Future<Output = ()> + Send + 'static,
pub fn on_upgrade<F, Fut>(self, callback: F) -> Response where
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.
Trait Implementations
sourceimpl Debug for WebSocketUpgrade
impl Debug for WebSocketUpgrade
sourceimpl<B> FromRequest<B> for WebSocketUpgrade where
B: Send,
impl<B> FromRequest<B> for WebSocketUpgrade where
B: Send,
type Rejection = WebSocketUpgradeRejection
type Rejection = WebSocketUpgradeRejection
If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response. Read more
sourcefn from_request<'life0, 'async_trait>(
req: &'life0 mut RequestParts<B>
) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait>> where
'life0: 'async_trait,
Self: 'async_trait,
fn from_request<'life0, 'async_trait>(
req: &'life0 mut RequestParts<B>
) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait>> where
'life0: 'async_trait,
Self: 'async_trait,
Perform the extraction.
Auto Trait Implementations
impl !RefUnwindSafe for WebSocketUpgrade
impl Send for WebSocketUpgrade
impl Sync for WebSocketUpgrade
impl Unpin for WebSocketUpgrade
impl !UnwindSafe for WebSocketUpgrade
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> PolicyExt for T where
T: ?Sized,
impl<T> PolicyExt for T where
T: ?Sized,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber to this type, returning a
WithDispatch wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber to this type, returning a
WithDispatch wrapper. Read more