pub struct WebSocket;Expand description
WebSocket protocol utilities.
Implementations§
Source§impl WebSocket
impl WebSocket
Sourcepub fn upgrade<F>(request: &Request, handler: F) -> Response
pub fn upgrade<F>(request: &Request, handler: F) -> Response
Answer a WebSocket upgrade request: returns the 101 response to
return from the handler, and runs handler with the connection once
it’s sent. A request that isn’t a valid upgrade (or comes over HTTP/2
or HTTP/3) gets 426 Upgrade Required and handler never runs.
use rust_web_server::app::App;
use rust_web_server::websocket::{Message, WebSocket};
let app = App::with_state(()).get("/echo", |request, _params, _conn, _state| {
WebSocket::upgrade(request, |mut ws| {
while let Ok(Some(message)) = ws.recv() {
let _ = match message {
Message::Text(text) => ws.send_text(&text),
Message::Binary(data) => ws.send_binary(&data),
};
}
})
});Source§impl WebSocket
impl WebSocket
Sourcepub const MAX_FRAME_PAYLOAD: u64
pub const MAX_FRAME_PAYLOAD: u64
The largest frame payload WebSocket::read_frame accepts (16 MiB);
a frame claiming more is an error. Use
WebSocket::read_frame_limited for another limit.
Sourcepub fn is_upgrade_request(request: &Request) -> bool
pub fn is_upgrade_request(request: &Request) -> bool
Returns true if request is a valid WebSocket upgrade request
(has Upgrade: websocket, Connection: upgrade, and Sec-WebSocket-Key).
Sourcepub fn handshake_response(request: &Request) -> Result<Response, String>
pub fn handshake_response(request: &Request) -> Result<Response, String>
Build the HTTP 101 Switching Protocols response for a WebSocket
opening handshake. Returns an error if Sec-WebSocket-Key is absent.
Write the raw bytes from Response::generate_response to the stream,
then transition to frame-level I/O with WebSocket::read_frame /
WebSocket::write_frame.
Sourcepub fn accept_key(client_key: &str) -> String
pub fn accept_key(client_key: &str) -> String
Compute the Sec-WebSocket-Accept value from the client’s
Sec-WebSocket-Key using SHA-1 and base64 as specified in RFC 6455.
Sourcepub fn read_frame(stream: &mut impl Read) -> Result<Frame, String>
pub fn read_frame(stream: &mut impl Read) -> Result<Frame, String>
Read one WebSocket frame from stream.
Handles client-to-server masking automatically. Returns an error if the stream closes unexpectedly or contains a protocol violation.
A frame whose declared payload exceeds WebSocket::MAX_FRAME_PAYLOAD
is refused before any of it is read: the length comes from the peer,
and allocating it up front (up to 2^64 − 1 bytes) aborted the process.
Sourcepub fn read_frame_limited(
stream: &mut impl Read,
max_payload: u64,
) -> Result<Frame, String>
pub fn read_frame_limited( stream: &mut impl Read, max_payload: u64, ) -> Result<Frame, String>
WebSocket::read_frame with max_payload bytes as the limit. The
payload is read as it arrives, so memory follows what the peer
actually sends, not what it claims.
Sourcepub fn write_frame(stream: &mut impl Write, frame: Frame) -> Result<(), String>
pub fn write_frame(stream: &mut impl Write, frame: Frame) -> Result<(), String>
Write a WebSocket frame to stream (server→client, unmasked).
Sourcepub fn send_text(stream: &mut impl Write, text: &str) -> Result<(), String>
pub fn send_text(stream: &mut impl Write, text: &str) -> Result<(), String>
Convenience: send a text message.
Auto Trait Implementations§
impl Freeze for WebSocket
impl RefUnwindSafe for WebSocket
impl Send for WebSocket
impl Sync for WebSocket
impl Unpin for WebSocket
impl UnsafeUnpin for WebSocket
impl UnwindSafe for WebSocket
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§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