Crates.io | coap-server |
lib.rs | coap-server |
version | 0.1.1 |
source | src |
created_at | 2022-04-28 03:46:49.896815 |
updated_at | 2022-05-04 02:31:34.9918 |
description | Robust async CoAP server |
homepage | |
repository | https://github.com/jasta/coap-server-rs |
max_upload_size | |
id | 576597 |
size | 142,373 |
An asynchronous CoAP server with a modern and ergonomic API for larger scale applications, inspired by warp and actix. CoAP offers an excellent alternative to HTTP for resource constrained environments like IoT devices.
use coap_server::app::{CoapError, Request, Response};
use coap_server::{app, CoapServer, FatalServerError, UdpTransport};
#[tokio::main]
async fn main() -> Result<(), FatalServerError> {
let server = CoapServer::bind(UdpTransport::new("0.0.0.0:5683")).await?;
server.serve(
app::new().resource(
app::resource("/hello").get(handle_get_hello))
).await
}
async fn handle_get_hello(request: Request<SocketAddr>) -> Result<Response, CoapError> {
let whom = request
.unmatched_path
.first()
.cloned()
.unwrap_or_else(|| "world".to_string());
let mut response = request.new_response();
response.message.payload = format!("Hello, {whom}").into_bytes();
Ok(response)
}
To experiment, I recommend using the excellent coap-client command-line tool, as with:
$ coap-client -m get coap://localhost/hello
Hello, world
See examples for more.
This project aims to be a robust and complete CoAP server, and in particular a more convenient alternative to MQTT for Rust-based projects:
/.well-known/core
(RFC 6690)Desired but not implemented:
/.well-known/core
filtering.