[][src]Struct websocket::server::upgrade::sync::HyperRequest

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

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();

Trait Implementations

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

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

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

Auto Trait Implementations

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

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

Blanket Implementations

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

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

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

impl<T, U> Into for T where
    U: From<T>, 
[src]

Performs the conversion.

impl<T> From for T
[src]

Performs the conversion.

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

impl<T> Borrow for T where
    T: ?Sized
[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut for T where
    T: ?Sized
[src]

Mutably borrows from an owned value. Read more

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

🔬 This is a nightly-only experimental API. (get_type_id)

this method will likely be replaced by an associated static

Gets the TypeId of self. Read more

impl<T> Typeable for T where
    T: Any
[src]

Get the TypeId of this object.

impl<T> Erased for T
[src]