mqttgo

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package mqttgo wraps Eclipse Paho MQTT Golang client with enhanced reconnection handling Provides simplified API for MQTT operations with mate ecosystem integration Solves automatic resubscription on reconnect using OnConnect callback pattern

mqttgo 包封装 Eclipse Paho MQTT Golang 客户端,增强重连处理能力 提供简化的 MQTT 操作接口,集成 mate 生态工具链 通过 OnConnect 回调模式解决重连时的自动重订阅问题

Package mqttgo wraps Eclipse Paho MQTT Golang client with enhanced reconnection handling Provides simplified API for MQTT operations with mate ecosystem integration Solves automatic resubscription on reconnect using OnConnect callback pattern

mqttgo 包封装 Eclipse Paho MQTT Golang 客户端,增强重连处理能力 提供简化的 MQTT 操作接口,集成 mate 生态工具链 通过 OnConnect 回调模式解决重连时的自动重订阅问题

Package mqttgo wraps Eclipse Paho MQTT Golang client with enhanced reconnection handling Provides simplified API for MQTT operations with mate ecosystem integration Solves automatic resubscription on reconnect using OnConnect callback pattern

mqttgo 包封装 Eclipse Paho MQTT Golang 客户端,增强重连处理能力 提供简化的 MQTT 操作接口,集成 mate 生态工具链 通过 OnConnect 回调模式解决重连时的自动重订阅问题

Package mqttgo wraps Eclipse Paho MQTT Golang client with enhanced reconnection handling Provides simplified API for MQTT operations with mate ecosystem integration Solves automatic resubscription on reconnect using OnConnect callback pattern

mqttgo 包封装 Eclipse Paho MQTT Golang 客户端,增强重连处理能力 提供简化的 MQTT 操作接口,集成 mate 生态工具链 通过 OnConnect 回调模式解决重连时的自动重订阅问题

Package mqttgo wraps Eclipse Paho MQTT Golang client with enhanced reconnection handling Provides simplified API for MQTT operations with mate ecosystem integration Solves automatic resubscription on reconnect using OnConnect callback pattern

mqttgo 包封装 Eclipse Paho MQTT Golang 客户端,增强重连处理能力 提供简化的 MQTT 操作接口,集成 mate 生态工具链 通过 OnConnect 回调模式解决重连时的自动重订阅问题

Package mqttgo wraps Eclipse Paho MQTT Golang client with enhanced reconnection handling Provides simplified API for MQTT operations with mate ecosystem integration Solves automatic resubscription on reconnect using OnConnect callback pattern

mqttgo 包封装 Eclipse Paho MQTT Golang 客户端,增强重连处理能力 提供简化的 MQTT 操作接口,集成 mate 生态工具链 通过 OnConnect 回调模式解决重连时的自动重订阅问题

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient

func NewClient(clientOptions *mqtt.ClientOptions) (mqtt.Client, error)

NewClient creates and connects an MQTT client with given options Enables automatic reconnection when connection drops Blocks until initial connection succeeds or fails with error

NewClient 使用给定选项创建并连接 MQTT 客户端 启用连接断开时的自动重连功能 阻塞直到初始连接成功或失败并返回错误

func NewClientOptions

func NewClientOptions(cfg *Config, clientID string) *mqtt.ClientOptions

NewClientOptions creates MQTT client options from configuration and client identifier Configures broker connection, authentication, keep-alive intervals and callbacks Sets default handlers for publish, connect and disconnect events

NewClientOptions 从配置和客户端标识符创建 MQTT 客户端选项 配置代理服务器连接、认证、保活间隔和回调函数 设置发布、连接和断开事件的默认处理程序

func NewClientWithCallback added in v0.0.2

func NewClientWithCallback(config *Config, clientID string, callback *Callback) (mqtt.Client, error)

NewClientWithCallback creates an MQTT client with automatic resubscription on reconnect Addresses the issue where Paho client does not auto-resubscribe after reconnection Solution based on https://github.com/eclipse/paho.mqtt.golang/issues/22 Implements OnConnect callback pattern for reliable subscription management

NewClientWithCallback 创建支持重连时自动重订阅的 MQTT 客户端 解决 Paho 客户端重连后不会自动重订阅的问题 解决方案基于 https://github.com/eclipse/paho.mqtt.golang/issues/22 实现 OnConnect 回调模式以可靠管理订阅

func OnConnectWithRetries added in v0.0.1

func OnConnectWithRetries(client mqtt.Client, onConnect func(client mqtt.Client, retryTimes uint64) (CallbackState, error))

OnConnectWithRetries runs a callback function with reattempt mechanism on connection events Continues reattempting until callback succeeds when client connection remains active Uses 100ms gap between attempts when operations need reattempts

OnConnectWithRetries 在连接事件上运行带重新尝试逻辑的回调函数 持续重新尝试直到回调成功或客户端断开连接 失败操作的重新尝试之间使用 100ms 延迟

func SetLog

func SetLog(mqttLog Log)

SetLog replaces the default logger with a custom implementation Allows integration with existing logging infrastructure Must be called before creating any MQTT clients to take effect

SetLog 用自定义实现替换默认日志记录器 允许与现有日志基础设施集成 必须在创建任何 MQTT 客户端之前调用才能生效

Types

type Callback added in v0.0.2

type Callback struct {
	// contains filtered or unexported fields
}

Callback manages OnConnect callback functions for automatic resubscription Stores multiple callback handlers that run when client connects or reconnects Supports chaining via fluent API pattern

Callback 管理用于自动重订阅的 OnConnect 回调函数 存储在客户端连接或重连时运行的多个回调处理程序 通过流式 API 模式支持链式调用

func NewCallback added in v0.0.2

func NewCallback() *Callback

NewCallback creates a new callback handler collection for connection events Returns empty callback ready for OnConnect handler registration

NewCallback 为连接事件创建新的回调处理程序集合 返回准备好注册 OnConnect 处理程序的空回调

func (*Callback) OnConnect added in v0.0.2

func (C *Callback) OnConnect(onConnect func(client mqtt.Client, retryTimes uint64) (CallbackState, error)) *Callback

OnConnect adds a callback handler to run on client connect and reconnect events Supports multiple handlers that execute in registration order Returns self for fluent method chaining

OnConnect 添加在客户端连接和重连事件上运行的回调处理程序 支持多个按注册顺序执行的处理程序 返回自身以支持流式方法链

type CallbackState added in v0.0.2

type CallbackState string

CallbackState represents the status of an OnConnect callback execution Controls repeat actions following connection setup and reconnection events Decides on continuing reattempts, timeout conditions and completion upon success

CallbackState 表示 OnConnect 回调执行的状态 控制连接建立或重连后的重复操作 决定是否继续重新尝试、超时还是成功完成

const (
	// CallbackUnknown indicates an unknown issue needing reattempt with backoff
	// Callback continues with 100ms gap between attempts
	//
	// CallbackUnknown 表示需要退避重新尝试的未知错误
	// 回调将以每次尝试之间 100ms 的延迟继续
	CallbackUnknown CallbackState = "unknown"

	// CallbackRetries indicates an explicit request to reattempt the callback
	// Analogous to Unknown yet gives explicit purpose when reattempting
	//
	// CallbackRetries 表示明确请求重新尝试回调
	// 类似于 Unknown 但为重新尝试逻辑提供明确意图
	CallbackRetries CallbackState = "retries"

	// CallbackTimeout indicates the callback should stop reattempting when timeout occurs
	// Ends the loop even when operation has not succeeded
	//
	// CallbackTimeout 表示回调因超时应停止重新尝试
	// 即使操作未成功也终止循环
	CallbackTimeout CallbackState = "timeout"

	// CallbackSuccess indicates the callback completed without issues
	// Stops the reattempt loop and confirms operation finished as expected
	//
	// CallbackSuccess 表示回调无错误完成
	// 停止重新尝试循环并确认操作按预期完成
	CallbackSuccess CallbackState = "success"
)

type Client

type Client = mqtt.Client

Client represents an MQTT client connection for publish and subscribe operations Alias to mqtt.Client from paho.mqtt.golang

Client 表示用于发布和订阅操作的 MQTT 客户端连接 对应 paho.mqtt.golang 的 mqtt.Client 类型别名

type Config

type Config struct {
	BrokerServer string // MQTT broker URL (e.g., tcp://host:port or ws://host:port) // 代理服务器地址(如 tcp://host:port 或 ws://host:port)
	Username     string // Authentication username for broker connection // 代理服务器连接的认证用户名
	Password     string // Authentication password for broker connection // 代理服务器连接的认证密码
	OrderMatters bool   // Whether message order matters in subscription handlers // 订阅处理程序中消息顺序是否重要
}

Config represents MQTT client settings for single or batch connections Contains authentication credentials and behavior parameters for broker communication Supports WebSocket connections via ws:// broker URLs

Config 表示单个或批量客户端的 MQTT 连接配置 包含代理服务器的认证凭据和行为参数 通过 ws:// 代理服务器地址支持 WebSocket 连接

type Log

type Log interface {
	ErrorLog(msg string, fields ...zap.Field) // Logs error-level messages with structured fields // 记录错误级别消息及结构化字段
	DebugLog(msg string, fields ...zap.Field) // Logs debug-level messages with structured fields // 记录调试级别消息及结构化字段
}

Log defines structured logging interface for MQTT operations and events Supports custom implementations via SetLog for integration with different frameworks Default implementation uses zaplog from mate ecosystem

Log 定义 MQTT 操作和事件的结构化日志接口 通过 SetLog 支持自定义实现,以集成不同的日志框架 默认实现使用 mate 生态的 zaplog

type Message

type Message = mqtt.Message

Message represents an MQTT message received from broker or to be published Contains topic, payload, QoS level and other metadata

Message 表示从代理服务器接收或待发布的 MQTT 消息 包含主题、负载、QoS 级别及其他元数据

type Token

type Token = mqtt.Token

Token represents an asynchronous operation handle for MQTT actions Used to track status of publish, subscribe and other operations

Token 表示 MQTT 操作的异步操作句柄 用于跟踪发布、订阅等操作的状态

type TokenState added in v0.0.1

type TokenState string

TokenState represents the completion status of an MQTT operation token Used to identify whether operations succeeded, timed out, or encountered errors

TokenState 表示 MQTT 操作令牌的完成状态 用于识别操作是成功、超时还是遇到错误

const (
	// TokenStateUnknown indicates an unknown error occurred during token operation
	// Usually means the operation failed with an unexpected condition
	//
	// TokenStateUnknown 表示令牌操作期间发生未知错误
	// 通常意味着操作因意外情况失败
	TokenStateUnknown TokenState = "unknown"

	// TokenStateTimeout indicates the token operation exceeded the allowed time limit
	// Returned when WaitTimeout expires before operation completes
	//
	// TokenStateTimeout 表示令牌操作超过允许的时间限制
	// 在 WaitTimeout 过期且操作未完成时返回
	TokenStateTimeout TokenState = "timeout"

	// TokenStateSuccess indicates the token operation completed without errors
	// Confirms the MQTT action finished as expected
	//
	// TokenStateSuccess 表示令牌操作成功完成且无错误
	// 确认 MQTT 操作按预期完成
	TokenStateSuccess TokenState = "success"
)

func CheckToken added in v0.0.1

func CheckToken(token mqtt.Token, timeout time.Duration) (TokenState, error)

CheckToken checks token completion with timeout and returns operation status Waits up to timeout duration for token to complete before returning Returns TokenStateSuccess if operation finishes without error

CheckToken 检查令牌完成情况并设置超时,返回操作状态 等待最多 timeout 时长以等令牌完成 若操作无错误完成则返回 TokenStateSuccess

func WaitToken added in v0.0.1

func WaitToken(token mqtt.Token) (TokenState, error)

WaitToken blocks until token operation completes and returns final status Does not timeout but waits for completion or client disconnect Returns TokenStateSuccess only when operation finishes without error

WaitToken 阻塞直到令牌操作完成并返回最终状态 不设超时但会等待完成或客户端断开 仅在操作无错误完成时返回 TokenStateSuccess

Jump to

Keyboard shortcuts

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