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.
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.
toml
webrocket = "0.1.0"
Since I am a big closure fan, many functions are offered as clousures, like onconnection or onmessage.
To start an echo server it only needs the following code:
```rust
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;
```
The server implementation was tested with the Autobahn|Testsuite as follows:
```bash $ RUSTLOG=debug cargo run --bin wsserverautobahn
$ 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
.
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.
brew install cmake
check compile options in flags.h (!)
```c
```
./build.sh run
./build.sh test [sha1]