[][src]Trait websocket::server::upgrade::sync::IntoWs

pub trait IntoWs {
    type Stream: Stream;
    type Error;
    fn into_ws(self) -> Result<Upgrade<Self::Stream>, Self::Error>;
}

Trait to take a stream or similar and attempt to recover the start of a websocket handshake from it. Should be used when a stream might contain a request for a websocket session.

If an upgrade request can be parsed, one can accept or deny the handshake with the WsUpgrade struct. Otherwise the original stream is returned along with an error.

Note: the stream is owned because the websocket client expects to own its stream.

This is already implemented for all Streams, which means all types with Read + Write.

Example

use std::net::TcpListener;
use std::net::TcpStream;
use websocket::sync::server::upgrade::IntoWs;
use websocket::sync::Client;

let listener = TcpListener::bind("127.0.0.1:80").unwrap();

for stream in listener.incoming().filter_map(Result::ok) {
    let mut client: Client<TcpStream> = match stream.into_ws() {
            Ok(upgrade) => {
            match upgrade.accept() {
                Ok(client) => client,
                Err(_) => panic!(),
            }
        },
            Err(_) => panic!(),
    };
}

Associated Types

The type of stream this upgrade process is working with (TcpStream, etc.)

An error value in case the stream is not asking for a websocket connection or something went wrong. It is common to also include the stream here.

Required Methods

Attempt to parse the start of a Websocket handshake, later with the returned WsUpgrade struct, call accept to start a websocket client, and reject to send a handshake rejection response.

Implementors

impl<'a, 'b> IntoWs for HyperRequest<'a, 'b>
[src]

impl<S> IntoWs for RequestStreamPair<S> where
    S: Stream
[src]

impl<S> IntoWs for S where
    S: Stream
[src]