pub struct HyperRequest<'a, 'b: 'a>(pub Request<'a, 'b>);
Expand description

Upgrade a hyper connection to a websocket one.

A hyper request is implicitly defined as a stream from other impls of Stream. Until trait impl specialization comes along, we use this struct to differentiate a hyper request (which already has parsed headers) from a normal stream.

Using this method, one can start a hyper server and check if each request is a websocket upgrade request, if so you can use websockets and hyper on the same port!

use hyper::server::{Server, Request, Response};
use websocket::Message;
use websocket::sync::server::upgrade::IntoWs;
use websocket::sync::server::upgrade::HyperRequest;

Server::http("0.0.0.0:80").unwrap().handle(move |req: Request, res: Response| {
    match HyperRequest(req).into_ws() {
        Ok(upgrade) => {
            // `accept` sends a successful handshake, no need to worry about res
            let mut client = match upgrade.accept() {
                Ok(c) => c,
                Err(_) => panic!(),
            };

            client.send_message(&Message::text("its free real estate"));
        },

        Err((request, err)) => {
            // continue using the request as normal, "echo uri"
            res.send(b"Try connecting over ws instead.").unwrap();
        },
    };
})
.unwrap();

Tuple Fields§

§0: Request<'a, 'b>

Trait Implementations§

source§

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

§

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

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

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

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.
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.

Auto Trait Implementations§

§

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

§

impl<'a, 'b> !RefUnwindSafe for HyperRequest<'a, 'b>

§

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

§

impl<'a, 'b> !Sync for HyperRequest<'a, 'b>

§

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

§

impl<'a, 'b> !UnwindSafe for HyperRequest<'a, 'b>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> Typeable for T
where T: Any,

source§

fn get_type(&self) -> TypeId

Get the TypeId of this object.