socket-flow
Simple async WebSockets implementation for Tokio stack.
Introduction
This library is supposed to offer a simple implementation for websockets, so end-user could use this to wrap a websockets server/client into their application, offering a smooth way of setting it up into your code.
It's an async library based on tokio runtime, which takes as argument a tokio TcpStream, using that stream of bytes to implement the standards of WebSocket Protocol RFC, performing handshake, reading frames, parsing masks and internal payload.
Most of the library internal and end-user communication, uses tokio mpsc for passing binary data, frames and errors. After
sending the TcpStream to our function, you receive WSConnection
struct, which has a read mpsc channel for reading data,
and some public methods for sending binary, text, ping and close frames.
The motivation behind this, was to offer a simple way of having a WebSockets connection over your application, using as a
reference wide established libraries, like tungstenite-rs
and tokio-tungstenite
Features
Most of all WebSockets RFC features are implemented, like:
- Handshake process, key parsing and generation
- OpCodes handling, like
Text
,Binary
,Ping
,Pong
andContinue
- Multiple subscriptions
- Scalability
- Error handling
Features to be added:
- Autobahn tests
- TLS/SSL support
Usage
Add this in your Cargo.toml
:
[]
= "*"
Example of usage
Here is a ping-pong server example, that you can also find in: Example
use *;
use perform_handshake;
use SocketAddr;
use ;
use select;
async
async
For running this example, you can clone the repo and execute:
cargo run --color=always --package socket-flow --example internal_server
This example, creates a TcpListener, binding it to a port, accepting connections, handling each of these connections
inside a tokio task, for handling clients concurrently. The handle_connection function, make sure the handshake process
is performed, returning a WSConnection
, which has a tokio mpsc channel, where you can consume incoming data for this client,
and perform write into the socket operations, including error handling through Result
.
You can check more examples over Examples