Struct WebSocketUpgrade
pub struct WebSocketUpgrade<F = DefaultOnFailedUpgrade> { /* private fields */ }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§
§impl<F> WebSocketUpgrade<F>
impl<F> WebSocketUpgrade<F>
pub fn read_buffer_size(self, size: usize) -> WebSocketUpgrade<F>
pub fn read_buffer_size(self, size: usize) -> WebSocketUpgrade<F>
Read buffer capacity. The default value is 128KiB
pub fn write_buffer_size(self, size: usize) -> WebSocketUpgrade<F>
pub fn write_buffer_size(self, size: usize) -> WebSocketUpgrade<F>
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.
pub fn max_write_buffer_size(self, max: usize) -> WebSocketUpgrade<F>
pub fn max_write_buffer_size(self, max: usize) -> WebSocketUpgrade<F>
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.
pub fn max_message_size(self, max: usize) -> WebSocketUpgrade<F>
pub fn max_message_size(self, max: usize) -> WebSocketUpgrade<F>
Set the maximum message size (defaults to 64 megabytes)
pub fn max_frame_size(self, max: usize) -> WebSocketUpgrade<F>
pub fn max_frame_size(self, max: usize) -> WebSocketUpgrade<F>
Set the maximum frame size (defaults to 16 megabytes)
pub fn accept_unmasked_frames(self, accept: bool) -> WebSocketUpgrade<F>
pub fn accept_unmasked_frames(self, accept: bool) -> WebSocketUpgrade<F>
Allow server to accept unmasked frames (defaults to false)
pub fn protocols<I>(self, protocols: I) -> WebSocketUpgrade<F>
pub fn protocols<I>(self, protocols: I) -> WebSocketUpgrade<F>
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 {
// ...
})
}pub fn requested_protocols(&self) -> impl Iterator<Item = &HeaderValue>
pub fn requested_protocols(&self) -> impl Iterator<Item = &HeaderValue>
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".
pub fn set_selected_protocol(&mut self, protocol: HeaderValue)
pub fn set_selected_protocol(&mut self, protocol: HeaderValue)
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.
pub fn selected_protocol(&self) -> Option<&HeaderValue>
pub fn selected_protocol(&self) -> Option<&HeaderValue>
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.
pub fn on_failed_upgrade<C>(self, callback: C) -> WebSocketUpgrade<C>where
C: OnFailedUpgrade,
pub fn on_failed_upgrade<C>(self, callback: C) -> WebSocketUpgrade<C>where
C: OnFailedUpgrade,
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 { /* ... */ })
}pub fn on_upgrade<C, Fut>(self, callback: C) -> Response<Body>
pub fn on_upgrade<C, Fut>(self, callback: C) -> Response<Body>
Finalize upgrading the connection and call the provided callback with the stream.
Trait Implementations§
§impl<F> Debug for WebSocketUpgrade<F>
impl<F> Debug for WebSocketUpgrade<F>
§impl<S> FromRequestParts<S> for WebSocketUpgrade
impl<S> FromRequestParts<S> for WebSocketUpgrade
§type Rejection = WebSocketUpgradeRejection
type Rejection = WebSocketUpgradeRejection
§async fn from_request_parts(
parts: &mut Parts,
_state: &S,
) -> Result<WebSocketUpgrade, <WebSocketUpgrade as FromRequestParts<S>>::Rejection>
async fn from_request_parts( parts: &mut Parts, _state: &S, ) -> Result<WebSocketUpgrade, <WebSocketUpgrade as FromRequestParts<S>>::Rejection>
Auto Trait Implementations§
impl<F = DefaultOnFailedUpgrade> !Freeze for WebSocketUpgrade<F>
impl<F> RefUnwindSafe for WebSocketUpgrade<F>where
F: RefUnwindSafe,
impl<F> Send for WebSocketUpgrade<F>where
F: Send,
impl<F> Sync for WebSocketUpgrade<F>where
F: Sync,
impl<F> Unpin for WebSocketUpgrade<F>where
F: Unpin,
impl<F> UnsafeUnpin for WebSocketUpgrade<F>where
F: UnsafeUnpin,
impl<F> UnwindSafe for WebSocketUpgrade<F>where
F: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<S, T> FromRequest<S, ViaParts> for T
impl<S, T> FromRequest<S, ViaParts> for T
§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
§fn from_request(
req: Request<Body>,
state: &S,
) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
fn from_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<'src, T> IntoMaybe<'src, T> for Twhere
T: 'src,
impl<'src, T> IntoMaybe<'src, T> for Twhere
T: 'src,
impl<T> MaybeSend for T
impl<T> MaybeSendSync for T
§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling [Attribute] value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi [Quirk] value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the [Condition] value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);