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