pub struct WebSocketContext<'ctx> { /* private fields */ }Expand description
example.rs
use mews::{WebSocketContext, Connection, Message};
async fn handle_websocket(
headers: Headers/* of upgrade request */,
tcp: TcpStream
) -> Response {
let ctx = WebSocketContext::new(
&headers["Sec-WebSocket-Key"]
);
let (sign, ws) = ctx.on_upgrade(
|mut conn: Connection<TcpStream>| async move {
while let Ok(Some(Message::Text(text))) = conn.recv().await {
conn.send(text).await
.expect("failed to send message");
sleep(Duration::from_secs(1)).await;
}
}
);
spawn(ws.manage(tcp));
/* return `Switching Protocol` response with `sign`... */
}Implementations§
Source§impl<'ctx> WebSocketContext<'ctx>
impl<'ctx> WebSocketContext<'ctx>
Sourcepub fn new(sec_websocket_key: &'ctx str) -> Self
pub fn new(sec_websocket_key: &'ctx str) -> Self
create WebSocketContext with Sec-WebSocket-Key request header value.
pub fn with(self, config: Config) -> Self
Sourcepub fn on_upgrade<C: UnderlyingConnection, H, F>(
self,
handler: H,
) -> (String, WebSocket<C>)
pub fn on_upgrade<C: UnderlyingConnection, H, F>( self, handler: H, ) -> (String, WebSocket<C>)
create Sec-WebSocket-Accept value and a WebSocket with the handler.
§handler
any ’static FnOnce(Connection<C>) -> {impl Future<Output = ()> + Send} + Send + Sync
Trait Implementations§
Source§impl Debug for WebSocketContext<'_>
impl Debug for WebSocketContext<'_>
Source§impl PartialEq for WebSocketContext<'_>
impl PartialEq for WebSocketContext<'_>
Auto Trait Implementations§
impl<'ctx> Freeze for WebSocketContext<'ctx>
impl<'ctx> RefUnwindSafe for WebSocketContext<'ctx>
impl<'ctx> Send for WebSocketContext<'ctx>
impl<'ctx> Sync for WebSocketContext<'ctx>
impl<'ctx> Unpin for WebSocketContext<'ctx>
impl<'ctx> UnwindSafe for WebSocketContext<'ctx>
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