Documentation ¶
Index ¶
- func GenClient(configs string, server ...string) error
- type Broker
- type MqttConfigs
- type MqttStub
- func (stub *MqttStub) Connect(opt *mq.ClientOptions) error
- func (stub *MqttStub) GenConfigs(mode ...string) *mq.ClientOptions
- func (stub *MqttStub) Notify(topic string, Qos ...byte) error
- func (stub *MqttStub) Publish(topic string, data any, Qos ...byte) error
- func (stub *MqttStub) PublishOptions(topic string, data any, remain bool, Qos ...byte) error
- func (stub *MqttStub) Subscribe(topic string, hanlder mq.MessageHandler, Qos ...byte) error
- type Options
- type User
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type MqttConfigs ¶
type MqttConfigs struct { Broker Broker `json:"broker"` Users map[string]*User `json:"users"` CAFile string `json:"ca"` CerFile string `json:"cert"` KeyFile string `json:"key"` }
MQTT configs pasered from nacos configs server
type MqttStub ¶
type MqttStub struct { Options *Options Client mq.Client ConnectHandler mq.OnConnectHandler // Connect callback handler DisconnectHandler mq.ConnectionLostHandler // Disconnect callback handler ReconnectHandler mq.ReconnectHandler // Reconnect callback handler MessageHandler mq.MessageHandler // Default publish message callback handler // contains filtered or unexported fields }
MQTT stub to manager MQTT connection.
As usages you can connect remote MQTT broker and get client instance by follow usecases.
UseCase 1 : Using nacos MQTT configs and connect without callbacks.
if err := mqtt.GenClient(data); err != nil { logger.E("Connect client err:", err) return }
UseCase 2 : Using nacos MQTT configs and connect with callbacks.
stub := mqtt.SetOptions(2) stub.ConnectHandler = ConnectHandler if err := mqtt.GenClient(data); err != nil { logger.E("Connect client err:", err) return }
UseCase 3 : Using singleton stub set custom configs and connect.
stub := mqtt.Singleton() // Here use stub.Client to set your client configs if err := stub.Connect(stub.GenConfigs()); err != nil { logger.E("Connect client err:", err) return }
func SetOptions ¶
Set default qos and remain flag
func (*MqttStub) Connect ¶
func (stub *MqttStub) Connect(opt *mq.ClientOptions) error
New client from given options and connect with broker
func (*MqttStub) GenConfigs ¶
func (stub *MqttStub) GenConfigs(mode ...string) *mq.ClientOptions
Generate mqtt config, default connection protocol using tcp, you can set mode 'tls' and cert files to using ssl protocol.
func (*MqttStub) PublishOptions ¶
Publish indicate topic message with input remain flag and Qos options,
Notice that the data will encode as json bytes array if value type is Struct, Pointer or map, or instead nil data to empty bytes array.
type Options ¶
type Options struct { Host string // remote MQTT broker host Port int // remote MQTT broker port number ClientID string // current client unique id on broker User *User // user account and password to connect broker CAFile string // CA cert file for TSL CerFile string // certificate/key file for TSL KeyFile string // secure key file for TSL }
Optionals mqtt client configs to connect MQTT broker