Struct WebSocketUpgrade
#[must_use]
pub struct WebSocketUpgrade<F = DefaultOnFailedUpgrade> { /* private fields */ }
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>
fn read_buffer_size(self, size: usize) -> SelfRead buffer capacity. The default value is 128KiB
fn write_buffer_size(self, size: usize) -> SelfThe 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
0each 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:
flushwill always fully write the buffer regardless.fn max_write_buffer_size(self, max: usize) -> SelfThe 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_sizewhen 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 messageand probably a little more depending on error handling strategy.fn max_message_size(self, max: usize) -> SelfSet the maximum message size (defaults to 64 megabytes)
fn max_frame_size(self, max: usize) -> SelfSet the maximum frame size (defaults to 16 megabytes)
fn accept_unmasked_frames(self, accept: bool) -> SelfAllow server to accept unmasked frames (defaults to false)
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-Protocolheader to match any of them, the upgrade response will includeSec-WebSocket-Protocolheader 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 ; let app = new.route; async # let _: Router = app;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".fn set_selected_protocol(&mut self, protocol: HeaderValue)Set the chosen WebSocket subprotocol.
Another method, [
protocols()][Self::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.
fn selected_protocol(&self) -> Option<&HeaderValue>Return the selected WebSocket subprotocol, if one has been chosen.
If [
protocols()][Self::protocols] selects a matching protocol, or [set_selected_protocol()][Self::set_selected_protocol] has been called, the return value will beSomecontaining the selected protocol. Otherwise, it will beNone.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 ; async # #fn on_upgrade<C, Fut>(self, callback: C) -> Response where C: FnOnce(WebSocket) -> Fut + Send + 'static, Fut: Future<Output = ()> + Send + 'static, F: OnFailedUpgrade,Finalize upgrading the connection and call the provided callback with the stream.
Trait Implementations
impl<F> Debug for WebSocketUpgrade<F>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<S> FromRequestParts<S> for WebSocketUpgrade<DefaultOnFailedUpgrade>
where
S: Send + Sync,
type Rejection = WebSocketUpgradeRejection;async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::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
impl<S, T> FromRequest<S, ViaParts> for WebSocketUpgrade<F>
where
S: Send + Sync,
T: FromRequestParts<S>,
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>>
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for WebSocketUpgrade<F>
where
ST: ?Sized,
DT: ?Sized,
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for WebSocketUpgrade<F>
where
ST: ?Sized,
DT: ?Sized,
impl<T> Any for WebSocketUpgrade<F>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for WebSocketUpgrade<F>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for WebSocketUpgrade<F>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for WebSocketUpgrade<F>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> Instrument for WebSocketUpgrade<F>
impl<T> Read<Exclusive, BecauseExclusive> for WebSocketUpgrade<F>
where
T: ?Sized,
impl<T> Same for WebSocketUpgrade<F>
type Output = T;
impl<T> WithSubscriber for WebSocketUpgrade<F>
impl<T, U> Into<U> for WebSocketUpgrade<F>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for WebSocketUpgrade<F>
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for WebSocketUpgrade<F>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
impl<V, T> VZip<V> for WebSocketUpgrade<F>
where
V: MultiLane<T>,
fn vzip(self) -> V