mqtt

package
v0.0.0-...-5049426 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenClient

func GenClient(configs string, server ...string) error

Generate mqtt client and connect with MQTT broker, the client using 'tcp' protocol and fixed id as format 'server@12345678'.

Types

type Broker

type Broker struct {
	Host string `json:"host"`
	Port int    `json:"port"`
}

MQTT broker address

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

func SetOptions(qos byte, remain ...bool) *MqttStub

Set default qos and remain flag

func Singleton

func Singleton() *MqttStub

Return Mqtt global singleton

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) Notify

func (stub *MqttStub) Notify(topic string, Qos ...byte) error

Publish empty message topic, it same use for just notify

func (*MqttStub) Publish

func (stub *MqttStub) Publish(topic string, data any, Qos ...byte) error

Publish indicate topic message, the Qos can be set current call in 0 ~ 2

func (*MqttStub) PublishOptions

func (stub *MqttStub) PublishOptions(topic string, data any, remain bool, Qos ...byte) error

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.

func (*MqttStub) Subscribe

func (stub *MqttStub) Subscribe(topic string, hanlder mq.MessageHandler, Qos ...byte) error

Subscribe given topic and set callback

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

type User

type User struct {
	Account  string `json:"user"`
	Password string `json:"pwd"`
}

MQTT client login user

Jump to

Keyboard shortcuts

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