[−][src]Struct roa::websocket::Websocket
This is supported on
feature="websocket"
only.The Websocket middleware.
Example
use futures::StreamExt; use roa::router::{Router, RouterError}; use roa::websocket::Websocket; use roa::{App, Context}; use roa::http::Method; let router = Router::new().on("/chat", Websocket::new(|_ctx, stream| async move { let (write, read) = stream.split(); // echo if let Err(err) = read.forward(write).await { println!("forward err: {}", err); } })); let app = App::new().end(router.routes("/")?); Ok(())
Parameter
- Context
The context is the same with roa context, however, neither read body from request or write anything to response is unavailing.
- SocketStream
The websocket stream, implementing Stream
and Sink
.
Return
Must be ()
, as roa cannot deal with errors occurring in websocket.
Methods
impl<F, S, Fut> Websocket<F, S, Fut> where
F: Fn(Context<S>, SocketStream) -> Fut,
[src]
F: Fn(Context<S>, SocketStream) -> Fut,
pub fn new(task: F) -> Self
[src]
This is supported on
feature="websocket"
only.Construct a websocket middleware by task closure.
pub fn with_config(config: WebSocketConfig, task: F) -> Self
[src]
This is supported on
feature="websocket"
only.Construct a websocket middleware with config.
Example
use futures::StreamExt; use roa::router::{Router, RouterError}; use roa::websocket::{Websocket, WebSocketConfig}; use roa::{App, Context}; use roa::http::Method; let router = Router::new().on("/chat", Websocket::with_config( WebSocketConfig::default(), |_ctx, stream| async move { let (write, read) = stream.split(); // echo if let Err(err) = read.forward(write).await { println!("forward err: {}", err); } }) ); let app = App::new().end(router.routes("/")?);
Trait Implementations
impl<'a, F, S, Fut> Endpoint<'a, S> for Websocket<F, S, Fut> where
S: State,
F: 'static + Sync + Send + Fn(Context<S>, SocketStream) -> Fut,
Fut: 'static + Send + Future<Output = ()>,
[src]
S: State,
F: 'static + Sync + Send + Fn(Context<S>, SocketStream) -> Fut,
Fut: 'static + Send + Future<Output = ()>,
fn call<'async_trait>(
&'a self,
ctx: &'a mut Context<S>
) -> Pin<Box<dyn Future<Output = Result<(), Status>> + 'async_trait>> where
'a: 'async_trait,
Self: 'async_trait,
[src]
&'a self,
ctx: &'a mut Context<S>
) -> Pin<Box<dyn Future<Output = Result<(), Status>> + 'async_trait>> where
'a: 'async_trait,
Self: 'async_trait,
impl<F, S, Fut> Send for Websocket<F, S, Fut> where
F: Sync + Send + Fn(Context<S>, SocketStream) -> Fut,
[src]
F: Sync + Send + Fn(Context<S>, SocketStream) -> Fut,
impl<F, S, Fut> Sync for Websocket<F, S, Fut> where
F: Sync + Send + Fn(Context<S>, SocketStream) -> Fut,
[src]
F: Sync + Send + Fn(Context<S>, SocketStream) -> Fut,
Auto Trait Implementations
impl<F, S, Fut> RefUnwindSafe for Websocket<F, S, Fut> where
F: RefUnwindSafe,
Fut: RefUnwindSafe,
S: RefUnwindSafe,
F: RefUnwindSafe,
Fut: RefUnwindSafe,
S: RefUnwindSafe,
impl<F, S, Fut> Unpin for Websocket<F, S, Fut> where
Fut: Unpin,
S: Unpin,
Fut: Unpin,
S: Unpin,
impl<F, S, Fut> UnwindSafe for Websocket<F, S, Fut> where
F: RefUnwindSafe,
Fut: UnwindSafe,
S: UnwindSafe,
F: RefUnwindSafe,
Fut: UnwindSafe,
S: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<'a, S, T, F> Endpoint<'a, S> for T where
F: 'a + Future<Output = Result<(), Status>>,
S: 'a,
T: 'static + Send + Sync + Fn(&'a mut Context<S>) -> F,
[src]
F: 'a + Future<Output = Result<(), Status>>,
S: 'a,
T: 'static + Send + Sync + Fn(&'a mut Context<S>) -> F,
fn call<'async_trait>(
&'a self,
ctx: &'a mut Context<S>
) -> Pin<Box<dyn Future<Output = Result<(), Status>> + 'async_trait>> where
'a: 'async_trait,
T: 'async_trait,
[src]
&'a self,
ctx: &'a mut Context<S>
) -> Pin<Box<dyn Future<Output = Result<(), Status>> + 'async_trait>> where
'a: 'async_trait,
T: 'async_trait,
impl<S, T> EndpointExt<S> for T where
T: for<'a> Endpoint<'a, S>,
[src]
T: for<'a> Endpoint<'a, S>,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,