pub struct WebSocketUpgrade { /* private fields */ }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§
Source§impl WebSocketUpgrade
impl WebSocketUpgrade
Sourcepub fn max_send_queue(self, max: usize) -> WebSocketUpgrade
pub fn max_send_queue(self, max: usize) -> WebSocketUpgrade
Set the size of the internal message send queue.
Sourcepub fn max_message_size(self, max: usize) -> WebSocketUpgrade
pub fn max_message_size(self, max: usize) -> WebSocketUpgrade
Set the maximum message size (defaults to 64 megabytes)
Sourcepub fn max_frame_size(self, max: usize) -> WebSocketUpgrade
pub fn max_frame_size(self, max: usize) -> WebSocketUpgrade
Set the maximum frame size (defaults to 16 megabytes)
Sourcepub fn protocols<I>(self, protocols: I) -> WebSocketUpgrade
pub fn protocols<I>(self, protocols: I) -> WebSocketUpgrade
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,
) -> Response<UnsyncBoxBody<Bytes, Error>>
pub fn on_upgrade<F, Fut>( self, callback: F, ) -> Response<UnsyncBoxBody<Bytes, Error>>
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.