contracts/mqtt.go

81 lines
2.0 KiB
Go
Raw Normal View History

2024-04-17 14:59:02 +08:00
package contracts
2024-04-24 09:44:46 +08:00
import (
"github.com/samber/do"
)
2024-04-17 14:59:02 +08:00
2024-04-23 09:49:28 +08:00
type MqttService interface {
2024-04-17 14:59:02 +08:00
Start() error
Stop() error
Restart() error
// 消息推送
2024-04-24 09:44:46 +08:00
//
// Example:
//
// Parameters:
// topic 订阅的主题
// msg 传递的消息
// retain 是否保留
// qos 传递的消息,
// 0: 至多一次, 如果发送失败,也就算了
// 1: 至少一次, 消息将确保至少被传递一次,但可能会重复发送
// 2: 确保只有一次, 确保消息仅被传递一次且没有重复传递
// Returns:
// error - 执行结果
2024-04-17 14:59:02 +08:00
Publish(topic string, msg any, retain bool, qos byte) error
// 关闭消息队列
Close(origin string)
}
2024-04-23 09:49:28 +08:00
type Mqtt interface {
2024-04-17 14:59:02 +08:00
Controller
2024-04-23 09:49:28 +08:00
// 链接
// OnConnect(g GlobalParams, topic string) error
// 断开链接
// OnDisconnect(g GlobalParams, topic string) error
2024-04-17 14:59:02 +08:00
// 订阅事件
2024-04-23 09:49:28 +08:00
OnSubscribed(g GlobalParams, topic string) error
2024-04-17 14:59:02 +08:00
// 取消订阅事件
2024-04-23 09:49:28 +08:00
OnUnsubscribed(g GlobalParams, topic string) error
2024-04-17 14:59:02 +08:00
// 消息发布事件
2024-04-24 09:44:46 +08:00
OnMessage(g GlobalParams, topic string, retain bool, qos byte) error
// 消息被丢弃
OnMessageDropped(g GlobalParams, topic string, retain bool, qos byte) error
2024-04-17 14:59:02 +08:00
// 保留类型消息事件
2024-04-24 09:44:46 +08:00
// OnRetainMessage(g GlobalParams, topic string) error
2024-04-17 14:59:02 +08:00
// Qos消息完成事件
2024-04-24 09:44:46 +08:00
// OnQosMessage(g GlobalParams, topic string) error
2024-04-17 14:59:02 +08:00
// 客户端超时事件
2024-04-24 09:44:46 +08:00
// OnClientExpired(GlobalParams) error
2024-04-17 14:59:02 +08:00
// 保留消息超时事件
2024-04-24 09:44:46 +08:00
// OnRetainedExpired(g GlobalParams, topic string) error
2024-04-17 14:59:02 +08:00
}
2024-04-23 09:49:28 +08:00
type MqttHandle struct {
2024-04-17 14:59:02 +08:00
Controller
}
2024-04-23 09:49:28 +08:00
func (this MqttHandle) OnSubscribed(g GlobalParams, topic string) error {
2024-04-17 14:59:02 +08:00
return nil
}
2024-04-23 09:49:28 +08:00
func (this MqttHandle) OnUnsubscribed(g GlobalParams, topic string) error {
2024-04-17 14:59:02 +08:00
return nil
}
2024-04-24 09:44:46 +08:00
func (this MqttHandle) OnMessage(g GlobalParams, topic string, retain bool, qos byte) error {
2024-04-17 14:59:02 +08:00
return nil
}
2024-04-24 09:44:46 +08:00
func (this MqttHandle) OnMessageDropped(g GlobalParams, topic string, retain bool, qos byte) error {
2024-04-17 14:59:02 +08:00
return nil
}
2024-04-23 09:49:28 +08:00
func NewMqttController(container *do.Injector) Mqtt {
return &MqttHandle{
2024-04-17 14:59:02 +08:00
Controller: &BaseController{container},
}
}