#mqtt #协议

mqtt-协议

MQTT 协议库

20 个版本 (11 个重大更新)

0.12.0 2024年3月13日
0.11.2 2022年2月8日
0.11.1 2021年10月20日
0.11.0 2021年3月29日
0.2.0 2016年7月23日

#224网络编程

Download history 162/week @ 2024-04-07 145/week @ 2024-04-14 172/week @ 2024-04-21 225/week @ 2024-04-28 162/week @ 2024-05-05 170/week @ 2024-05-12 219/week @ 2024-05-19 199/week @ 2024-05-26 140/week @ 2024-06-02 174/week @ 2024-06-09 211/week @ 2024-06-16 174/week @ 2024-06-23 226/week @ 2024-06-30 199/week @ 2024-07-07 177/week @ 2024-07-14 251/week @ 2024-07-21

866 每月下载
用于 9 个包 (4 个直接使用)

MIT/Apache

115KB
2.5K SLoC

MQTT-rs

Build Status Build & Test License crates.io dependency status

Rust 的 MQTT 协议库

[dependencies]
mqtt-protocol = "0.12"

用法

extern crate mqtt;

use std::io::Cursor;

use mqtt::{Encodable, Decodable};
use mqtt::packet::{VariablePacket, PublishPacket, QoSWithPacketIdentifier};
use mqtt::TopicName;

fn main() {
    // Create a new Publish packet
    let packet = PublishPacket::new(TopicName::new("mqtt/learning").unwrap(),
                                    QoSWithPacketIdentifier::Level2(10),
                                    "Hello MQTT!");

    // Encode
    let mut buf = Vec::new();
    packet.encode(&mut buf).unwrap();
    println!("Encoded: {:?}", buf);

    // Decode it with known type
    let mut dec_buf = Cursor::new(&buf[..]);
    let decoded = PublishPacket::decode(&mut dec_buf).unwrap();
    println!("Decoded: {:?}", decoded);
    assert_eq!(packet, decoded);

    // Auto decode by the fixed header
    let mut dec_buf = Cursor::new(&buf[..]);
    let auto_decode = VariablePacket::decode(&mut dec_buf).unwrap();
    println!("Variable packet decode: {:?}", auto_decode);
    assert_eq!(VariablePacket::PublishPacket(packet), auto_decode);
}

注意

依赖项

~0.5–2.3MB
~44K SLoC