pub struct WebSocket { /* private fields */ }Expand description
WebSocket extractor for upgrading HTTP connections to WebSocket
Use this extractor in your handler to initiate a WebSocket upgrade.
The extractor validates the upgrade request and returns a WebSocket
that can be used to set up the connection handler.
§Example
ⓘ
use rustapi_ws::{WebSocket, Message};
async fn ws_handler(ws: WebSocket) -> impl IntoResponse {
ws.on_upgrade(|socket| async move {
let (mut sender, mut receiver) = socket.split();
while let Some(Ok(msg)) = receiver.next().await {
match msg {
Message::Text(text) => {
// Echo back
let _ = sender.send(Message::text(format!("Echo: {}", text))).await;
}
Message::Close(_) => break,
_ => {}
}
}
})
}Implementations§
Source§impl WebSocket
impl WebSocket
Sourcepub fn on_upgrade<F, Fut>(self, callback: F) -> WebSocketUpgrade
pub fn on_upgrade<F, Fut>(self, callback: F) -> WebSocketUpgrade
Create a WebSocket upgrade response with a handler
The provided callback will be called with the established WebSocket stream once the upgrade is complete.
Sourcepub fn has_protocol(&self, protocol: &str) -> bool
pub fn has_protocol(&self, protocol: &str) -> bool
Check if a specific protocol was requested
Trait Implementations§
Source§impl FromRequest for WebSocket
impl FromRequest for WebSocket
Source§async fn from_request(req: &mut Request) -> Result<Self>
async fn from_request(req: &mut Request) -> Result<Self>
Extract from the full request
Source§impl OperationModifier for WebSocket
impl OperationModifier for WebSocket
fn update_operation(_op: &mut Operation)
Auto Trait Implementations§
impl Freeze for WebSocket
impl RefUnwindSafe for WebSocket
impl Send for WebSocket
impl Sync for WebSocket
impl Unpin 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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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