Trait IntoWs

Source
pub trait IntoWs {
    type Stream: Stream;
    type Error;

    // Required method
    fn into_ws(self) -> Result<Upgrade<Self::Stream>, Self::Error>;
}
Expand description

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!(),
    };
}

Required Associated Types§

Source

type Stream: Stream

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

Source

type Error

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§

Source

fn into_ws(self) -> Result<Upgrade<Self::Stream>, Self::Error>

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§

Source§

impl<'a, 'b> IntoWs for HyperRequest<'a, 'b>

Source§

type Stream = &'a mut &'b mut (dyn NetworkStream + 'static)

Source§

type Error = (Request<'a, 'b>, HyperIntoWsError)

Source§

impl<S> IntoWs for RequestStreamPair<S>
where S: Stream,

Source§

impl<S> IntoWs for S
where S: Stream,