mqttclient

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxReconnectInterval = time.Second * 60
	DefaultQueueSize            = 100
)

定义常量和错误

Variables

View Source
var (
	ErrNilHandler         = errors.New("handler cannot be nil")
	ErrClientClosed       = errors.New("client is closed")
	ErrInvalidConfig      = errors.New("invalid configuration")
	ErrSubscriptionFailed = errors.New("subscription failed")
	ErrConnectionFailed   = errors.New("connection failed")
	ErrPublishFailed      = errors.New("publish failed")
	ErrUnsubscribeFailed  = errors.New("unsubscribe failed")
)

Functions

func LogLevelToInt

func LogLevelToInt(level LogLevel) int

LogLevelToInt 将 LogLevel 枚举转换为 int

Types

type Client

type Client struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Client 实现MQTT客户端

func NewClient

func NewClient(ctx context.Context, conf Config) (*Client, error)

NewClient 创建新的MQTT客户端实例

func (*Client) Close

func (c *Client) Close() error

Close 关闭客户端连接

func (*Client) GetClient

func (c *Client) GetClient() paho.Client

GetClient 获取底层的paho.Client

func (*Client) GetHandlerByTopic

func (c *Client) GetHandlerByTopic(topic string) (Handler, bool)

GetHandlerByTopic 获取指定主题的处理器

func (*Client) IsConnected

func (c *Client) IsConnected() bool

IsConnected 检查客户端是否已连接

func (*Client) Publish

func (c *Client) Publish(topic string, qos byte, data []byte) error

Publish 发布消息

func (*Client) RegisterHandler

func (c *Client) RegisterHandler(handler Handler) error

RegisterHandler 注册消息处理器

func (*Client) SetLogLevel

func (c *Client) SetLogLevel(level LogLevel)

SetLogLevel 设置日志级别

func (*Client) SetLogger

func (c *Client) SetLogger(logger Logger)

SetLogger 设置新的日志记录器

func (*Client) UnregisterHandler

func (c *Client) UnregisterHandler(topic string) error

UnregisterHandler 注销消息处理器

type Config

type Config struct {
	Server               string        // MQTT broker地址
	Username             string        // 用户名
	Password             string        // 密码
	MaxReconnectInterval time.Duration // 重连间隔
	QOS                  uint8         // 服务质量等级
	CleanSession         bool          // 清理会话标志
	ClientID             string        // 客户端ID
	CAFile               string        // CA证书文件
	CertFile             string        // 客户端证书
	CertKeyFile          string        // 客户端密钥
	Logger               Logger        // 日志记录器
	LogLevel             LogLevel      // 日志级别
	QueueSize            int           // 消息队列大小
}

Config 定义客户端配置

type Handler

type Handler struct {
	Topic  string              // 订阅主题
	Qos    byte                // 订阅QoS级别
	Handle paho.MessageHandler // 消息处理函数
}

Handler 定义订阅数据处理器

type LogLevel

type LogLevel int

LogLevel 定义日志级别

const (
	// LogLevelNone 不输出日志
	LogLevelNone LogLevel = iota
	// LogLevelError 只输出错误日志
	LogLevelError
	// LogLevelWarn 输出警告和错误日志
	LogLevelWarn
	// LogLevelInfo 输出信息、警告和错误日志
	LogLevelInfo
	// LogLevelDebug 输出所有日志
	LogLevelDebug
)

func IntToLogLevel

func IntToLogLevel(level int) LogLevel

IntToLogLevel 将 int 转换为 LogLevel 枚举

type Logger

type Logger interface {
	Debug(format string, v ...interface{})
	Info(format string, v ...interface{})
	Warn(format string, v ...interface{})
	Error(format string, v ...interface{})
}

Logger 定义日志接口

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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