contracts/mqtt.go

87 lines
2.3 KiB
Go

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