xmqtt

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

README

xmqtt

mqtt 封装工具库

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Ops     *mqtt.ClientOptions
	Timeout time.Duration // connection、subscribe、publish Timeout time, default 10s
	Mqtt    mqtt.Client
	Topics  []string
	SubFunc smap.Map[string, mqtt.MessageHandler] // key:topic#qos, value: callback func
	// contains filtered or unexported fields
}

func New

func New(c *Config) (mc *Client)

New 1、New

func (*Client) Close

func (c *Client) Close()

Close 主动断开连接

func (*Client) DefaultOnConnectFunc

func (c *Client) DefaultOnConnectFunc(cli mqtt.Client)

func (*Client) OnConnectListener

func (c *Client) OnConnectListener(fun mqtt.OnConnectHandler) (mc *Client)

OnConnectListener 2、设置链接监听

func (*Client) OnConnectLostListener

func (c *Client) OnConnectLostListener(fun mqtt.ConnectionLostHandler) (mc *Client)

OnConnectLostListener 3、设置断开链接监听

func (*Client) Publish

func (c *Client) Publish(topic string, qos QosType, payload any) error

Publish 推送消息

func (*Client) RegisterConsumers

func (c *Client) RegisterConsumers(consumers []*Consumer)

RegisterConsumers 批量注册消费者

func (*Client) StartAndConnect

func (c *Client) StartAndConnect() (err error)

StartAndConnect 4、真实创建Client并连接mqtt

func (*Client) Subscribe

func (c *Client) Subscribe(topic string, qos QosType, callback mqtt.MessageHandler) error

Subscribe 订阅topic

func (*Client) UnSubscribe

func (c *Client) UnSubscribe(topics ...string) error

UnSubscribe 取消订阅topic

type Config

type Config struct {
	Broker       string         `json:"broker" yaml:"broker" toml:"broker"`
	TcpPort      int            `json:"tcp_port" yaml:"tcp_port" toml:"tcp_port"`
	ClientId     string         `json:"client_id" yaml:"client_id" toml:"client_id"`
	Uname        string         `json:"uname" yaml:"uname" toml:"uname"`
	Password     string         `json:"password" yaml:"password" toml:"password"`
	KeepAlive    xtime.Duration `json:"keep_alive" yaml:"keep_alive" toml:"keep_alive"` // 单位秒
	Timeout      xtime.Duration `json:"Timeout" yaml:"Timeout" toml:"Timeout"`
	CleanSession bool           `json:"clean_session" yaml:"clean_session" toml:"clean_session"`
}

type Consumer

type Consumer struct {
	Topic    string
	QosType  QosType
	Callback mqtt.MessageHandler
}

type QosType

type QosType byte
const (
	QosAtMostOne  QosType = 0
	QosAtLeastOne QosType = 1
	QosOnlyOne    QosType = 2
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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