Crates.io | ws-endpoint |
lib.rs | ws-endpoint |
version | 0.1.0 |
source | src |
created_at | 2020-07-08 20:23:11.675136 |
updated_at | 2020-07-08 20:23:11.675136 |
description | A WebSocket API endpoint implemented in Rust |
homepage | |
repository | https://gitlab.com/bbmsoft.net/ws-endpoint/ |
max_upload_size | |
id | 262634 |
size | 39,884 |
A generic asynchronous tokio and tungstenite based web socket endpoint implementation. This is not actually a real library but more a building block for websocket based application that wants to provide a request/response API.
To use it, just create a new WsEndpoint instance with an appropriate processor (a function taking in a request, e.g. as a JSON String, and returning an encoded response) and start it with the socket address you want it to run at.
Here's a minimal example (may be out of date, check the 'echo' example for the latest version):
extern crate ws_endpoint;
use std::io;
use ws_endpoint::WsEndpoint;
#[tokio::main]
async fn main() -> io::Result<()> {
let endpoint = WsEndpoint::new_sync_text_endpoint(process);
endpoint.start("127.0.0.1:5000").await
}
async fn process(request: String) -> Option<String> {
// We are just echoing here.
// In a real world scenario this would be
// the place to parse and process the
// request and return a proper response.
let response = request;
Some(response)
}