Documentation
¶
Overview ¶
Package mqtt 提供 MQTT 客户端集成(设备网关采集数据场景): 连接 Broker、订阅主题、发布消息、断线自动重连(基于 paho.mqtt.golang)。
Index ¶
- func SetGlobal(client *Client)
- type Client
- func (c *Client) Close()
- func (c *Client) IsConnected() bool
- func (c *Client) Native() mqttlib.Client
- func (c *Client) Publish(topic string, qos byte, retained bool, payload []byte) error
- func (c *Client) PublishJSON(topic string, qos byte, value interface{}) error
- func (c *Client) Subscribe(topic string, qos byte, handler MessageHandler) error
- func (c *Client) Unsubscribe(topics ...string) error
- type Config
- type MessageHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client MQTT 客户端。
func NewClient ¶
func NewClient(config Config, onMessage MessageHandler) (*Client, error)
NewClient 创建并连接 MQTT Broker(连接失败立即报错)。 onMessage 为全局默认消息回调(所有未单独指定 handler 的订阅共用)。
func (*Client) PublishJSON ¶
PublishJSON 发布 JSON 消息(设备网关上报数据常用)。
type Config ¶
type Config struct {
// Broker 地址(如 tcp://127.0.0.1:1883 或 ssl://host:8883)。
Broker string
// ClientID 客户端标识(网关/服务唯一;空则自动生成)。
ClientID string
// Username/Password 认证(可选)。
Username string
Password string
// CleanSession 会话清理(默认 true;false 时断线保留订阅)。
CleanSession bool
// Timeout 连接超时(默认 10s)。
Timeout time.Duration
// AutoReconnect 断线自动重连(默认 true,paho 内置)。
AutoReconnect bool
}
Config MQTT 客户端配置。
type MessageHandler ¶
MessageHandler 消息回调(订阅处理)。
Click to show internal directories.
Click to hide internal directories.