#websocket #websocket-server #sha-1 #base64 #closures #scratch

bin+lib webrocket

A closure focused WebSocket server implementation

1 unstable release

0.1.0 Jan 26, 2023

#997 in Asynchronous

Custom license

43KB
1K SLoC

webrocket

WebRocket 🚀 is a WebSocket server library programmed in Rust from scratch (including SHA-1 and Base64).

Note: This project was created to learn Rust, so major changes may still occur.

installation

To add a library release version from crates.io to a Cargo project, add this to the 'dependencies' section of your Cargo.toml:

Please do not use in production yet.

webrocket = "0.1.0"

usage

Since I am a big closure fan, many functions are offered as clousures, like on_connection or on_message.

To start an echo server it only needs the following code:


let mut ws = WebSocket::bind("0.0.0.0:3000").await?;

ws.on_connection(|wsc| {
    println!("New connection");

    wsc.on_message(|wsc, msg| {
        println!("New message: {}", msg);
        wsc.send_message(msg);
    });

    wsc.on_close(|_, code, reason| {
        println!("Connection closed ({:?}) with '{}' as reason.", code, reason);
    });
});

ws.listen().await;

tests

The server implementation was tested with the Autobahn|Testsuite as follows:

$ RUST_LOG=debug cargo run --bin wsserver_autobahn

$ docker run -it --rm --net=host \
  -v "${PWD}/tests:/config" \
  -v "${PWD}/tests/reports:/reports" \
  --name fuzzingclient \
  crossbario/autobahn-testsuite \
  wstest -m fuzzingclient --spec /config/fuzzingclient.json

There are also tests in the code, which can be started with cargo test.

standards

already implemented

still to make

Side project

Before I wrote the WebSocket in Rust, I first programmed it in C++ to teach myself C++. I now use this implementation, which is in the fun-with-cpp branch, as a playground for e.g. fuzzing.

requirements (cpp)

  • CMake 3.22.1 brew install cmake
  • A C++17 compatibler compiler

check compile options in flags.h (!)

#define COMPILE_FOR_FUZZING 0
#define ARTIFICIAL_BUGS     0

build & run

./build.sh run

build & test

./build.sh test [sha1]

Dependencies

~6–15MB
~177K SLoC