Giter VIP home page Giter VIP logo

hyper-tungstenite-rs's Introduction

Docs.rs CI

hyper-tungstenite

This crate allows hyper servers to accept websocket connections, backed by tungstenite.

The upgrade function allows you to upgrade a HTTP connection to a websocket connection. It returns a HTTP response to send to the client, and a future that resolves to a WebSocketStream. The response must be sent to the client for the future to be resolved. In practise this means that you must spawn the future in a different task.

Note that the upgrade function itself does not check if the request is actually an upgrade request. For simple cases, you can check this using the is_upgrade_request function before calling upgrade. For more complicated cases where the server should support multiple upgrade protocols, you can manually inspect the Connection and Upgrade headers.

Example

use futures::{sink::SinkExt, stream::StreamExt};
use hyper::{Body, Request, Response};
use hyper_tungstenite::{tungstenite, HyperWebsocket};
use std::convert::Infallible;
use tungstenite::Message;

type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

/// Handle a HTTP or WebSocket request.
async fn handle_request(mut request: Request<Body>) -> Result<Response<Body>, Error> {
    // Check if the request is a websocket upgrade request.
    if hyper_tungstenite::is_upgrade_request(&request) {
        let (response, websocket) = hyper_tungstenite::upgrade(&mut request, None)?;

        // Spawn a task to handle the websocket connection.
        tokio::spawn(async move {
            if let Err(e) = serve_websocket(websocket).await {
                eprintln!("Error in websocket connection: {}", e);
            }
        });

        // Return the response so the spawned future can continue.
        Ok(response)
    } else {
        // Handle regular HTTP requests here.
        Ok(Response::new(Body::from("Hello HTTP!")))
    }
}

/// Handle a websocket connection.
async fn serve_websocket(websocket: HyperWebsocket) -> Result<(), Error> {
    let mut websocket = websocket.await?;
    while let Some(message) = websocket.next().await {
        match message? {
            Message::Text(msg) => {
                println!("Received text message: {}", msg);
                websocket.send(Message::text("Thank you, come again.")).await?;
            },
            Message::Binary(msg) => {
                println!("Received binary message: {:02X?}", msg);
                websocket.send(Message::binary(b"Thank you, come again.".to_vec())).await?;
            },
            Message::Ping(msg) => {
                // No need to send a reply: tungstenite takes care of this for you.
                println!("Received ping message: {:02X?}", msg);
            },
            Message::Pong(msg) => {
                println!("Received pong message: {:02X?}", msg);
            }
            Message::Close(msg) => {
                // No need to send a reply: tungstenite takes care of this for you.
                if let Some(msg) = &msg {
                    println!("Received close message with code {} and message: {}", msg.code, msg.reason);
                } else {
                    println!("Received close message");
                }
            },
            Message::Frame(msg) => {
               unreachable!();
            }
        }
    }

    Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    let addr: std::net::SocketAddr = "[::1]:3000".parse()?;
    println!("Listening on http://{}", addr);
    let listener = tokio::net::TcpListener::bind(&addr).await?;
    println!("listening on {}", addr);

    let mut http = hyper::server::conn::Http::new();
    http.http1_only(true);
    http.http1_keep_alive(true);

    loop {
        let (stream, _) = listener.accept().await?;
        let connection = http
            .serve_connection(stream, hyper::service::service_fn(handle_request))
            .with_upgrades();
        tokio::spawn(async move {
            if let Err(err) = connection.await {
                println!("Error serving HTTP connection: {:?}", err);
            }
        });
    }
}

hyper-tungstenite-rs's People

Contributors

daoneluna avatar de-vri-es avatar erwandl avatar lambdap avatar lukaskalbertodt avatar luqmana avatar micmine avatar omjadas avatar ravenclaw900 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

hyper-tungstenite-rs's Issues

WebSocketConfig is used in the public API but not reexported

Hello! I'm prefacing this by saying I don't need this, I just wanted to bring it to your attention. If you look here on line 127 we see that there is a WebSocketConfig type in the public API. However, this is not reexported like some other types as seen here. Which leads to these docs showing a type that can't be used in the upgrade function. If one puts tungstenite = "^0.14.0" in their Cargo.toml then this type can be used, but tungstenite is currently on "0.15.0" and people might find it confusing when trying to figure it out. The error message when this version mismatch occurs at least somewhat points the way. I'm guessing this was just oversight or something and there is a work around if it's needed, but I wanted to let you know!

Improve error handling in `upgrade`?

Hi there and thanks for the library!

I wanted to check what errors can be returned from upgrade and saw this:

let response = Response::builder()
.status(hyper::StatusCode::SWITCHING_PROTOCOLS)
.header(hyper::header::CONNECTION, "upgrade")
.header(hyper::header::UPGRADE, "websocket")
.header("Sec-WebSocket-Accept", &convert_key(key.as_bytes()))
.body(Body::from("switching to websocket protocol"))?;

I think the code shouldn't use ? at the end. As far as I understand the response builder of hyper, the body only returns a Result if the builder was used incorrectly, i.e. specifying bad header values. See the docs:

This function may return an error if any previously configured argument failed to parse or get converted to the internal representation. For example if an invalid head was specified via header("Foo", "Bar\r\n") the error will be returned when this function is called rather than when header was called.

So I think an unwrap or expect is actually more appropriate in this code as an error there would indicate a bug in the function. And for bugs, panics are used; Err(_) is only for recoverable errors.

In that case, the function could also just return a ProtocolError instead? Being more specific here would be nice from a perspective of a user of this library.

Ultimately, I want to decide: what to do in case of an error? I have to respond something. Well, I could just send nothing, but that's also not great. So if I'm understanding update correctly, an Err(_) would indicate a client error. Meaning I should respond with some 4xx code.

I can send a PR if you agree with this reasoning.

send message to all connected users

I have a separate thread that retrieves data. When this data is updated I want to relay this to all connected users. What is the correct way of storing the connected connections and send the message to them?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.