Documentation
¶
Index ¶
- Variables
- func CloseAllClients()
- func CloseClient(clientID string)
- func GetClient(opts *mqtt.ClientOptions) (client mqtt.Client, err error)
- func GetClientOptions(conf *Config) (*mqtt.ClientOptions, error)
- func Init(opts *mqtt.ClientOptions) (err error)
- func OnConnectHandler(handler mqtt.OnConnectHandler) mqtt.OnConnectHandler
- func Publish(topic string, payload any, qos byte, retained bool) (err error)
- func PublishAsync(topic string, payload any, qos byte, retained bool)
- func PublishWithTimeout(topic string, payload any, qos byte, retained bool, timeout time.Duration) (err error)
- func ResubscribeAll()
- func Subscribe(item SubscribeType)
- type BatchPublisher
- type ClientPool
- type Config
- type Message
- type SubscribeType
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrTimeout = errors.New("operation timeout")
自定义错误
Functions ¶
func GetClient ¶
func GetClient(opts *mqtt.ClientOptions) (client mqtt.Client, err error)
GetClient 获取MQTT连接(支持连接池)
func GetClientOptions ¶
func GetClientOptions(conf *Config) (*mqtt.ClientOptions, error)
GetClientOptions 获取MQTT连接配置项
func OnConnectHandler ¶ added in v1.2.0
func OnConnectHandler(handler mqtt.OnConnectHandler) mqtt.OnConnectHandler
OnConnectHandler 连接上服务器的操作
func PublishAsync ¶ added in v1.2.0
PublishAsync 异步发布消息(不等待确认)
Types ¶
type BatchPublisher ¶ added in v1.2.0
type BatchPublisher struct {
// contains filtered or unexported fields
}
BatchPublisher 批量发布器
func NewBatchPublisher ¶ added in v1.2.0
func NewBatchPublisher() *BatchPublisher
NewBatchPublisher 创建批量发布器
func (*BatchPublisher) AddMessage ¶ added in v1.2.0
func (bp *BatchPublisher) AddMessage(topic string, payload any, qos byte, retained bool)
AddMessage 添加消息到批量发布
func (*BatchPublisher) PublishBatch ¶ added in v1.2.0
func (bp *BatchPublisher) PublishBatch() error
PublishBatch 批量发布消息
func (*BatchPublisher) PublishBatchAsync ¶ added in v1.2.0
func (bp *BatchPublisher) PublishBatchAsync()
PublishBatchAsync 异步批量发布(不等待确认)
type ClientPool ¶ added in v1.2.0
type ClientPool struct {
// contains filtered or unexported fields
}
type Config ¶
type Config struct {
Broker string // Broker地址,例如tcp://127.0.0.1:1883或ssl://127.0.0.1:8883. 如果配置ssl,则必须配置CACert
Username string // 用户名,可选
Password string // 密码,可选
CACert string // CA证书,单向认证只需要配置此文件即可,无需ClientCert和ClientKey,可选
ClientCert string // ClientCert,可选
ClientKey string // ClientKey,可选
ClientID string // ClientID,可选
}
Config MQTT的配置信息格式
type SubscribeType ¶
type SubscribeType struct {
Topic string
Qos byte
Callback mqtt.MessageHandler
RetryTimes int // 为0表示无限重试
}
Click to show internal directories.
Click to hide internal directories.