Files
arenax-server/utility/mqtt/mqtt.go

24 lines
627 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package mqtt
// MqttClient 定义了通用 MQTT 客户端接口
type MqttClient interface {
// Publish 发布消息到指定 topic
Publish(topic string, payload []byte) error
// Subscribe 订阅指定 topic接收回调
Subscribe(topic string, handler func(topic string, payload []byte)) error
}
// 全局注册表
var clients = make(map[string]MqttClient)
// Register 注册一个 MQTT 客户端实现
func Register(name string, client MqttClient) {
clients[name] = client
}
// GetClient 获取已注册的客户端
func GetClient(name string) (MqttClient, bool) {
client, ok := clients[name]
return client, ok
}