[][src]Struct roa::websocket::Websocket

pub struct Websocket<F, S, Fut> where
    F: Fn(Context<S>, SocketStream) -> Fut, 
{ /* fields omitted */ }
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]

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]

impl<F, S, Fut> Send for Websocket<F, S, Fut> where
    F: Sync + Send + Fn(Context<S>, SocketStream) -> Fut, 
[src]

impl<F, S, Fut> Sync for Websocket<F, S, Fut> where
    F: Sync + Send + Fn(Context<S>, SocketStream) -> Fut, 
[src]

Auto Trait Implementations

impl<F, S, Fut> RefUnwindSafe for Websocket<F, S, Fut> where
    F: RefUnwindSafe,
    Fut: RefUnwindSafe,
    S: RefUnwindSafe

impl<F, S, Fut> Unpin for Websocket<F, S, Fut> where
    Fut: Unpin,
    S: Unpin

impl<F, S, Fut> UnwindSafe for Websocket<F, S, Fut> where
    F: RefUnwindSafe,
    Fut: UnwindSafe,
    S: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[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]

impl<S, T> EndpointExt<S> for T where
    T: for<'a> Endpoint<'a, S>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

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]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,