[][src]Type Definition websocket::server::upgrade::async::Upgrade

type Upgrade<S> = WsUpgrade<S, BytesMut>;

An asynchronous websocket upgrade.

This struct is given when a connection is being upgraded to a websocket request. It implements everything that a normal WsUpgrade struct does along with the final functions to create websocket clients (although this is done asynchronously).

Example

use websocket::async::{Core, TcpListener, TcpStream};
use websocket::async::futures::{Stream, Future};
use websocket::async::server::upgrade::IntoWs;
use websocket::sync::Client;

let mut core = Core::new().unwrap();
let handle = core.handle();
let addr = "127.0.0.1:80".parse().unwrap();
let listener = TcpListener::bind(&addr, &handle).unwrap();

let websocket_clients = listener
    .incoming().map_err(|e| e.into())
    .and_then(|(stream, _)| stream.into_ws().map_err(|e| e.3))
    .map(|upgrade| {
        if upgrade.protocols().iter().any(|p| p == "super-cool-proto") {
            let accepted = upgrade
                .use_protocol("super-cool-proto")
                .accept()
                .map(|_| ()).map_err(|_| ());

            handle.spawn(accepted);
        } else {
            let rejected = upgrade.reject()
                .map(|_| ()).map_err(|_| ());

            handle.spawn(rejected);
        }
    });