| Crates.io | ws-rs |
| lib.rs | ws-rs |
| version | 0.1.3 |
| created_at | 2025-05-18 15:04:58.710583+00 |
| updated_at | 2025-05-18 15:21:34.638838+00 |
| description | WebSocket library for OCPP communications with TLS support |
| homepage | |
| repository | https://github.com/ocpp-rs/ocpp-ws.git |
| max_upload_size | |
| id | 1678709 |
| size | 88,921 |
A secure WebSocket library for communications with TLS support, built in Rust.
Add the following to your Cargo.toml:
[dependencies]
ws-rs = { version = "0.1.0", features = ["client", "server"] }
use ws_rs::server::{WebSocketServer, ServerConfig};
use std::path::PathBuf;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize logger
env_logger::init();
// Configure server
let config = ServerConfig {
address: "127.0.0.2:8080".to_string(),
cert_path: PathBuf::from("certs/b_cert.pem"),
key_path: PathBuf::from("certs/b_key.pem"),
ca_cert_path: PathBuf::from("certs/ca_cert.pem"),
};
// Create and run server
let server = WebSocketServer::new(config);
server.run().await?;
Ok(())
}
use ws_rs::client::{WebSocketClient, ClientConfig};
use std::path::PathBuf;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize logger
env_logger::init();
// Configure client
let config = ClientConfig {
url: "wss://127.0.0.2:8080".to_string(),
cert_path: PathBuf::from("certs/a_cert.pem"),
key_path: PathBuf::from("certs/a_key.pem"),
ca_cert_path: PathBuf::from("certs/ca_cert.pem"),
};
// Connect to server
let client = WebSocketClient::new(config);
let connection = client.connect().await?;
// Use the connection
// ...
Ok(())
}
This library expects TLS certificates for secure communication. You can use the companion crate_cert tool to generate test certificates:
cd ../crate_cert && python main.py
The generated certificates should be placed in a certs directory with the following structure:
certs/
|- ca_cert.pem # CA certificate
|- a_cert.pem # Client certificate
|- a_key.pem # Client private key
|- b_cert.pem # Server certificate
|- b_key.pem # Server private key
This library uses the log crate for logging. To enable logging, configure an implementation such as env_logger:
// Initialize with default settings
env_logger::init();
// Or with a specific log level
std::env::set_var("RUST_LOG", "info");
env_logger::init();
Control log levels using the RUST_LOG environment variable:
error: Only errorswarn: Warnings and errorsinfo: General information (default)debug: Debug informationtrace: All logsThis project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)