Skip to content

Commit c676979

Browse files
committed
publish crate
1 parent dec6b88 commit c676979

File tree

3 files changed

+159
-2
lines changed

3 files changed

+159
-2
lines changed

examples/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ name = "simple_client"
1414
path = "simple_client.rs"
1515

1616
[dev-dependencies]
17-
websocket-std = { version = "0.0.2", path = "../websocket" }
17+
websocket-std = { version = "0.0.3", path = "../websocket" }

websocket/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ authors = ["Alejandro Palomino <alejandropalomino22@gmail.com>"]
66
rust-version = "1.65.0"
77

88
license = "MIT"
9-
readme = "../README.md"
9+
readme = "README.md"
1010
repository = "https://github.com/alekay2200/websocket-std"
1111
description = """
1212
Websocket implementation using std support, focus on microcontrollers and interoperability with other languages like C through the ffi.

websocket/README.md

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# websocket-std
2+
3+
This is a WebSocket implementation in Rust that utilizes the standard library. Its goal is to function on low-resource devices such as microcontrollers, although it can also be applied in high-performance computer programs. I started the project for three main reasons:
4+
- Learn rust.
5+
- Publish a library in crates.io.
6+
- How to use the library from other languages.
7+
8+
The project is currently in beta phase, and while it is functional, further work is needed for the final version. Since I am a sole developer and cannot dedicate as much time as I would like, I have decided to publish it for those who want to try it out and contribute to the project [CONTRIBUTING.md](./CONTRIBUTING.md).
9+
10+
You can use the library in the following ways:
11+
12+
- In any Rust project that allows the use of the standard library, such as ``esp-rs`` with ``std`` support. Check out the [esp-rs docs](https://esp-rs.github.io/book/overview/using-the-standard-library.html) for more information.
13+
- In any C project, as it has a compatible FFI (Foreign Function Interface). You’ll need to compile the project as a static library and link it appropriately. Refer to this guide ([static lib usage](./ffi/README.md)) for more details.
14+
15+
**Feel free to explore the project and contribute! 🚀**
16+
17+
---
18+
19+
## Static library
20+
In the ``ffi/`` folder you will find the ``websocket-std.h`` header and a compiled static library for the xtensa architecture of the esp32 microcontroller from espressif.
21+
22+
You can use this static library in your esp idf and arduino projects for esp32. Check [ffi/xtensa-esp32-idf](./ffi/xtensa-esp32-espidf/README.md) for more information.
23+
24+
## Examples
25+
26+
The [examples](./examples/) folder contains various examples of how to use ``websocket-std``.
27+
28+
## Features
29+
30+
### Sync Client
31+
32+
The sync client manage an internal event loop, when you call a function to perform a websocket operation (``init``, ``send``, ...)
33+
it will be queued and as soon as you call the ``event_loop`` function it will perform one input (something was received)
34+
and one output (something to send to server) operations in one execution.
35+
36+
You can also use ``threads`` to work with the library. Check [examples](./examples/) for more information.
37+
38+
#### What works
39+
- Send text messages.
40+
- Handle received text messages.
41+
- Handle on connection events.
42+
- Handle on close events.
43+
- Work with websocket protocols.
44+
- Set the maximun length of the text that the websocket will send for each dataframe.
45+
46+
#### Comming
47+
- Websocket over SSL.
48+
- Send and receive binary data.
49+
- Websocket extensions.
50+
51+
### Sync Server
52+
53+
I'm planning also to introduce in the library a ``sync server`` following the same philosophy as the sync client.
54+
55+
---
56+
57+
## MCUs Tested
58+
59+
- ``ESP32`` using **esp-rs** with std support
60+
- ``ESP32`` using ``arduino`` framework in a ``PlatformIO`` project. (Should also work with esp-idf proyects).
61+
62+
---
63+
64+
# Test
65+
66+
Since is my first rust big project I started using the following tools for testing and code coveragera, but I would like to
67+
define another way of doing that because the test coverage reports are not the bests. I'm open to hear better ways of doing testing in rust.
68+
69+
## Execute all test
70+
71+
72+
```console
73+
cargo test
74+
```
75+
76+
## Generate coverage report
77+
78+
### Requirements
79+
80+
#### Install ``grcov`` tool
81+
82+
1. Install the llvm-tools or llvm-tools-preview component
83+
```console
84+
rustup component add llvm-tools-preview
85+
```
86+
87+
2. Ensure that the following environment variable is set up
88+
```console
89+
export RUSTFLAGS="-Cinstrument-coverage"
90+
```
91+
92+
3. Ensure each test runs gets its own profile information by defining the LLVM_PROFILE_FILE environment variable (%p will be replaced by the process ID, and %m by the binary signature)
93+
```console
94+
export LLVM_PROFILE_FILE="websocket-std-%p-%m.profraw"
95+
```
96+
97+
4. Install grcov
98+
99+
```console
100+
cargo install grcov
101+
```
102+
103+
### Generate report
104+
105+
Ensure that there isn't compilation or test errors.
106+
1. Build the code
107+
```console
108+
cargo build
109+
```
110+
111+
2. Run tests and ensure that all are ``OK``
112+
```console
113+
cargo test
114+
```
115+
116+
3. Be sure that the variables are exported.
117+
- RUSTFLAGS
118+
- LLVM_PROFILE_FILE
119+
120+
4. Generate coverage report as HTML
121+
```console
122+
grcov . --binary-path ./target/debug/deps/ -s . -t html --branch --ignore-not-existing --ignore '../*' --ignore "/*" -o target/coverage --excl-line grcov-excl-line
123+
```
124+
125+
The report will be generated at ``target/coverage/``, open ``index.html`` with a browser to see the results.
126+
127+
---
128+
129+
## Python websocket server
130+
131+
Since the library doesn't have a way to create websocket servers, here you will find an echo server example in python to test
132+
the client.
133+
134+
### Requirements
135+
- pip install websockets==11.0.3
136+
137+
### Code
138+
```python
139+
import asyncio
140+
from websockets.server import serve
141+
142+
HOST = "0.0.0.0"
143+
PORT = 3000
144+
protocol = ["superchat", "app", "chat"]
145+
146+
async def echo(websocket):
147+
async for message in websocket:
148+
if websocket.open:
149+
await websocket.send(message)
150+
151+
async def main():
152+
async with serve(echo, HOST, PORT, subprotocols=protocol):
153+
print(f"Websocket server running on: {HOST}:{PORT}")
154+
await asyncio.Future() # run forever
155+
156+
asyncio.run(main())
157+
```

0 commit comments

Comments
 (0)