29 releases

0.11.3 Sep 4, 2021
0.11.1 Jul 20, 2021
0.9.2 Jan 23, 2021
0.9.1 Dec 19, 2020
0.1.1 Jul 25, 2015

#75 in Network programming

Download history 20/week @ 2021-05-30 45/week @ 2021-06-06 71/week @ 2021-06-13 90/week @ 2021-06-20 48/week @ 2021-06-27 53/week @ 2021-07-04 49/week @ 2021-07-11 166/week @ 2021-07-18 117/week @ 2021-07-25 144/week @ 2021-08-01 99/week @ 2021-08-08 106/week @ 2021-08-15 77/week @ 2021-08-22 68/week @ 2021-08-29 53/week @ 2021-09-05 101/week @ 2021-09-12

374 downloads per month
Used in coap-message-demos

MIT license

73KB
1.5K SLoC

coap-rs

Travis Build Status Windows Build Status Downloads Coverage Status MIT licensed

A fast and stable Constrained Application Protocol(CoAP) library implemented in Rust.

Features:

Documentation

Installation

First add this to your Cargo.toml:

[dependencies]
coap = "0.11"

Example

Server:

#![feature(async_closure)]

use coap_lite::{RequestType as Method};
use coap::Server;
use tokio::runtime::Runtime;

fn main() {
    let addr = "127.0.0.1:5683";

    Runtime::new().unwrap().block_on(async move {
        let mut server = Server::new(addr).unwrap();
        println!("Server up on {}", addr);
        
        server.run(async move |request| {
            match request.get_method() {
                &Method::Get => println!("request by get {}", request.get_path()),
                &Method::Post => println!("request by post {}", String::from_utf8(request.message.payload).unwrap()),
                &Method::Put => println!("request by put {}", String::from_utf8(request.message.payload).unwrap()),
                _ => println!("request by other method"),
            };
            
            return match request.response {
                Some(mut message) => {
                    message.message.payload = b"OK".to_vec();
                    Some(message)
                },
                _ => None
            };
        }).await.unwrap();
    });
}

Client:

use coap::CoAPClient;

fn main() {
    let url = "coap://127.0.0.1:5683/Rust";
    println!("Client request: {}", url);

    let response = CoAPClient::get(url).unwrap();
    println!("Server reply: {}", String::from_utf8(response.message.payload).unwrap());
}

Benchmark

$ cargo bench

Dependencies

~9MB
~197K SLoC