quic-mqtt

module
v0.0.0-...-43f3d60 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2025 License: MIT

README

QUIC-MQTT

这个项目实现了基于 QUIC 协议的 MQTT 通信,使用 github.com/quic-go/quic-go 作为 github.com/eclipse/paho.mqtt.golang 的传输层。QUIC (Quick UDP Internet Connections) 相比 TCP 为 MQTT 提供了多项优势:

  • 多路复用连接,允许在单个连接上传输多个 MQTT 流
  • 内置 TLS 安全性
  • 缩短连接建立时间
  • 改进的拥塞控制
  • 在复杂网络环境下有更好的性能表现

项目组件

  • transport: 为 Paho MQTT 客户端实现的 QUIC 传输层
  • client: 使用 QUIC 传输层的 MQTT 客户端
  • server: 基于 QUIC 的简单 MQTT 代理服务器
  • examples: 演示如何使用该实现的示例应用

系统要求

  • Go 1.23 或更高版本 (由 quic-go 库所需)

使用方法

启动代理服务器
cd examples/broker
go run main.go

这将在 0.0.0.0:18831 上启动一个使用 QUIC 传输层的 MQTT 代理服务器。

订阅主题
cd examples/subscriber
go run main.go

这将连接到代理服务器并订阅 example/topic 主题。

发布消息
cd examples/publisher
go run main.go

这将连接到代理服务器并每 5 秒向 example/topic 主题发布一条消息。

在自己的代码中使用本库

客户端
import (
    "crypto/tls"
    "QUIC-MQTT/client"
)

// 创建 TLS 配置
tlsConfig := &tls.Config{
    InsecureSkipVerify: true, // 开发环境跳过证书验证
}

// 创建新的 QUIC MQTT 客户端
mqttClient, err := client.NewQUICMQTTClient("localhost:18831", "your-client-id", tlsConfig)
if err != nil {
    // 处理错误
}

// 连接到代理服务器
if err := mqttClient.Connect(); err != nil {
    // 处理错误
}
defer mqttClient.Disconnect()

// 订阅主题
mqttClient.Subscribe("your/topic", 0, yourMessageHandler)

// 发布消息
mqttClient.Publish("your/topic", 0, false, "Hello MQTT over QUIC!")
服务器端
import (
    "QUIC-MQTT/server"
)

// 创建新的 QUIC 代理服务器(第二个参数为 true 表示开发环境跳过证书验证)
broker, err := server.NewQUICBroker("0.0.0.0:18831", true)
if err != nil {
    // 处理错误
}

// 启动代理服务器
if err := broker.Start(); err != nil {
    // 处理错误
}

特性和优化

  • 基于 QUIC 的传输层实现了可靠的消息传递
  • 完整支持 MQTT 的发布/订阅模式
  • 内置 TLS 安全性
  • 自动重连机制
  • 详细的日志记录
  • 完整的错误处理

当前限制

本实现是一个基本的概念验证,还有一些限制:

  • 代理服务器中的 MQTT 数据包处理相对简化
  • 代理服务器中的 QoS 支持有限
  • 不支持持久会话
  • 不支持 MQTT v5 特性
  • 为了简化示例,当前使用的是自签名证书和跳过证书验证

排障指南

如果遇到问题,请参阅 TROUBLESHOOTING.md 文件来获取常见问题解决方案。

许可证

MIT 许可证

Directories

Path Synopsis
examples
broker command
publisher command
subscriber command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL