xmqtt

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2024 License: MIT Imports: 12 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ConnectEvent    = "ConnectEvent"
	DisconnectEvent = "DisconnectEvent"
	NullEvent       = "NullEvent"
)

Variables

View Source
var MqttList = CreateSafeMQTTList()

MqttList MQTT 客户端列表

View Source
var MqttRespOption = &MqttRespOptionType{
	SuccessCode: 1000,
	ErrorCode:   1001,
	SuccessMsg:  "SUCCESS!!!",
	ErrorMsg:    "ERROR!!!",
}
View Source
var SendDefaultQos byte = 0

Functions

func CreateClient

func CreateClient(optionHandler func(*ClientCallBackOption, *Config))

CreateClient 创建客户端

Types

type Client

type Client struct {
	IsInit bool
	// Cfg 配置
	Cfg *Config
	// Client 客户端
	Client *mqtt.Client
	// MessageCallbackFunc 接收到消息时触发函数
	MessageCallbackFunc MessageHandler
	// OnConnectCallBackFunc 连接成功时触发函数
	OnConnectCallBackFunc mqtt.OnConnectHandler
}

Client 客户端结构对象

func (*Client) Json

func (t *Client) Json() *MqttResp

func (*Client) Run

func (t *Client) Run()

func (*Client) SendMsg

func (t *Client) SendMsg(msg any, topic string, qos ...byte) error

func (*Client) SetMessageCallbackFunc

func (t *Client) SetMessageCallbackFunc(fn MessageHandler) *Client

SetMessageCallbackFunc 接收到消息时触发函数

func (*Client) SetOnConnectCallBackFunc

func (t *Client) SetOnConnectCallBackFunc(fn mqtt.OnConnectHandler) *Client

type ClientCallBackOption

type ClientCallBackOption struct {
	// MessageCallbackFunc 接收到消息时触发函数
	MessageCallbackFunc MessageHandler
	// OnConnectCallBackFunc 连接成功时触发函数
	OnConnectCallBackFunc mqtt.OnConnectHandler
}

ClientCallBackOption 客户端回调设置

type Config

type Config struct {
	Name         string `json:"name"`         // name 名称
	Debug        bool   `json:"debug"`        // debug 调试模式
	MqttUrl      string `json:"mqttUrl"`      // mqtt 链接地址
	ClientId     string `json:"clientId"`     // 客户端 id
	Subscribe    string `json:"subscribe"`    // 订阅地址
	Qos          byte   `json:"qos"`          // qos
	Username     string `json:"username"`     // 用户名
	Password     string `json:"password"`     // 密码
	Ping         int    `json:"ping"`         // ping 频率
	CleanSession bool   `json:"cleanSession"` // cleanSession
}

type EventHandlerData

type EventHandlerData struct {
	MsgHandlerData *MessageHandlerData
	EventData      any
}

EventHandlerData 事件处理数据

func (*EventHandlerData) GetJson

func (t *EventHandlerData) GetJson() (json *gjson.Json)

GetJson 获取 JSON 对象

func (*EventHandlerData) SendMsg

func (t *EventHandlerData) SendMsg(msg any, topic string, qos ...byte) error

SendMsg 发送消息

type MessageHandler

type MessageHandler func(handlerData *MessageHandlerData)

MessageHandler 消息处理函数

type MessageHandlerData

type MessageHandlerData struct {
	// XMQTT 本扩展 MQTT 操作对象
	XMQTT *Client
	// OMQTT 原始 MQTT 操作对象
	OMQTT mqtt.Client
	// Message 接收到的消息操作对象
	Message mqtt.Message
}

func (*MessageHandlerData) GetEvent

func (t *MessageHandlerData) GetEvent() (eventName string, data any, err error)

GetEvent 获取事件

func (*MessageHandlerData) GetMessageId

func (t *MessageHandlerData) GetMessageId() uint16

GetMessageId 获取 messageId

func (*MessageHandlerData) GetMsg

func (t *MessageHandlerData) GetMsg() string

GetMsg 获取接收到的消息内容

func (*MessageHandlerData) GetTopic

func (t *MessageHandlerData) GetTopic() string

GetTopic 获取 topic

type MqttResp

type MqttResp struct {
	// contains filtered or unexported fields
}

MqttResp MQTT 返回数据

func CreateMqttResp

func CreateMqttResp(client *Client) *MqttResp

func (*MqttResp) Err

func (t *MqttResp) Err() *MqttResp

func (*MqttResp) Resp

func (t *MqttResp) Resp(topic, command string, advanced ...func(data map[string]any) map[string]any)

func (*MqttResp) SetCode

func (t *MqttResp) SetCode(code int) *MqttResp

func (*MqttResp) SetData

func (t *MqttResp) SetData(data any) *MqttResp

func (*MqttResp) SetMsg

func (t *MqttResp) SetMsg(msg string) *MqttResp

type MqttRespOptionType

type MqttRespOptionType struct {
	SuccessCode int    `json:"successCode"`
	ErrorCode   int    `json:"errorCode"`
	SuccessMsg  string `json:"successMsg"`
	ErrorMsg    string `json:"errorMsg"`
}

type SafeMQTTList

type SafeMQTTList struct {
	M map[string]*Client
	// contains filtered or unexported fields
}

SafeMQTTList 安全 MQTT 列表

func CreateSafeMQTTList

func CreateSafeMQTTList() *SafeMQTTList

CreateSafeMQTTList 创建 安全 MQTT 列表

func (*SafeMQTTList) Get

func (t *SafeMQTTList) Get(mqttName string) *Client

Get 获取 MQTT 客户端对象

func (*SafeMQTTList) Set

func (t *SafeMQTTList) Set(mqttName string, client *Client)

Set 设置 MQTT 客户端对象

type SystemConnectEvent

type SystemConnectEvent struct {
	Event    string `json:"event"`
	ClientId string `json:"clientId"`
}

SystemConnectEvent 系统客户端连接事件

func IsSystemConnectEvent

func IsSystemConnectEvent(message, topic string) (*SystemConnectEvent, bool, error)

IsSystemConnectEvent 是否为监听系统连接事件

func (*SystemConnectEvent) IsCurrentService

func (t *SystemConnectEvent) IsCurrentService(clientId string) bool

IsCurrentService 是否为当前服务

Jump to

Keyboard shortcuts

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