41个版本 (17个重大变更)

0.18.0 2024年6月22日
0.16.0 2024年4月5日
0.15.0 2024年3月27日
0.13.0 2023年9月30日
0.1.1 2015年7月25日

#386网络编程

Download history 2020/week @ 2024-04-27 1380/week @ 2024-05-04 2043/week @ 2024-05-11 1848/week @ 2024-05-18 1541/week @ 2024-05-25 2653/week @ 2024-06-01 1800/week @ 2024-06-08 1790/week @ 2024-06-15 2455/week @ 2024-06-22 1948/week @ 2024-06-29 2570/week @ 2024-07-06 2390/week @ 2024-07-13 3405/week @ 2024-07-20 2308/week @ 2024-07-27 2391/week @ 2024-08-03 1986/week @ 2024-08-10

10,434 每月下载量
2 crates 中使用

MIT 许可证

145KB
3.5K SLoC

coap-rs

CircleCI Windows Build Status Downloads Coverage Status MIT licensed

一个快速稳定的基于Rust实现的受限应用协议(CoAP)库。

特性

文档

安装

首先在您的 Cargo.toml

[dependencies]
coap = "0.18"
coap-lite = "0.11.3"
tokio = {version = "^1.32", features = ["full"]}

示例

服务器

 use coap_lite::{RequestType as Method, CoapRequest};
 use coap::Server;
 use tokio::runtime::Runtime;
 use std::net::SocketAddr;
 fn main() {
     let addr = "127.0.0.1:5683";
 	Runtime::new().unwrap().block_on(async move {
         let mut server = Server::new_udp(addr).unwrap();
         println!("Server up on {}", addr);

         server.run(|mut request: Box<CoapRequest<SocketAddr>>| async {
             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.clone()).unwrap()),
                 &Method::Put => println!("request by put {}", String::from_utf8(request.message.payload.clone()).unwrap()),
                 _ => println!("request by other method"),
             };

             match request.response {
                 Some(ref mut message) => {
                     message.message.payload = b"OK".to_vec();

                 },
                 _ => {}
             };
             return request
         }).await.unwrap();
     });
 }

客户端

 use coap_lite::{RequestType as Method, CoapRequest};
 use coap::{UdpCoAPClient};
 use tokio::main;
 #[tokio::main]
 async fn main() {
     let url = "coap://127.0.0.1:5683/Rust";
     println!("Client request: {}", url);

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

基准测试

$ cargo bench

依赖项

~8–24MB
~369K SLoC