websocket-rs

Crates.iowebsocket-rs
lib.rswebsocket-rs
version0.1.4
sourcesrc
created_at2020-06-08 17:23:03.563538
updated_at2020-06-17 21:59:04.404913
descriptionDesigned for https://github.com/euvoor/hawk
homepagehttps://github.com/euvoor/hawk
repositoryhttps://github.com/euvoor/hawk/tree/master/daemon/websocket
max_upload_size
id251439
size21,065
Oussama Elgoumri (euvoor)

documentation

https://docs.rs/websocket-rs

README

Designed for HAWK

Example

use tokio::net::TcpListener;

use http_rs::Http;
use websocket_rs::{ Websocket, Opcode };

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut listener = TcpListener::bind("127.0.0.1:8080").await?;

    println!("Listening for websocket connections on 127.0.0.1:8080");

    while let Ok((stream, addr)) = listener.accept().await {
        tokio::spawn(async move {
            println!("New connection from {:?}", addr);

            let mut http = Http::new(stream);

            if let Some(req) = http.next().await {
                if req.for_websocket() {
                    let mut ws = Websocket::new_with_key(
                        http.transport.into_inner(),
                        req.headers.get("sec-websocket-key").unwrap().to_string()
                    );

                    while let Some(msg) = ws.next().await {
                        println!("{:?}", msg);

                        if msg.opcode == Opcode::Text {
                            let response = format!("Received: {:?}", msg.payload);
                            ws.send_text(response).await.unwrap();
                        }
                    }

                    println!("Client from {:?} disconnected", addr);
                }
            }
        });
    }

    Ok(())
}

TODO

  • [*] Handle errors properly
  • Receive opcode TEXT
  • Pong when Ping is received
  • Handle all payload lengths (le 125, =126, =127)
  • Mask key
  • Close websocket connection
  • Read fragmented frames
  • Extract websocket to its own crate
  • Add binary support
  • Send fragmented frames when the size reaches a treshold
  • Schedule ping/pong
  • Pong with application data included in ping
  • Keep track of connected clients
Commit count: 0

cargo fmt