mqtt-protocol

Crates.iomqtt-protocol
lib.rsmqtt-protocol
version0.12.0
sourcesrc
created_at2016-04-17 06:03:49.173913
updated_at2024-03-13 14:13:26.662232
descriptionMQTT Protocol Library
homepage
repositoryhttps://github.com/zonyitoo/mqtt-rs
max_upload_size
id4773
size144,868
ty (zonyitoo)

documentation

https://docs.rs/mqtt-protocol

README

MQTT-rs

Build Status Build & Test License crates.io dependency status

MQTT protocol library for Rust

[dependencies]
mqtt-protocol = "0.12"

Usage

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);
}

Note

Commit count: 135

cargo fmt