[feat] mqtt 支持

This commit is contained in:
what 2024-04-23 09:49:28 +08:00
parent 0e21627c9a
commit 7d55b17630
2 changed files with 24 additions and 18 deletions

40
mqtt.go
View File

@ -2,7 +2,7 @@ package contracts
import "github.com/samber/do" import "github.com/samber/do"
type Mqtt interface { type MqttService interface {
Start() error Start() error
Stop() error Stop() error
Restart() error Restart() error
@ -21,58 +21,62 @@ type Mqtt interface {
Close(origin string) Close(origin string)
} }
type MqttController interface { type Mqtt interface {
Controller Controller
// 链接
// OnConnect(g GlobalParams, topic string) error
// 断开链接
// OnDisconnect(g GlobalParams, topic string) error
// 订阅事件 // 订阅事件
OnSubscribed(GlobalParams) error OnSubscribed(g GlobalParams, topic string) error
// 取消订阅事件 // 取消订阅事件
OnUnsubscribed(GlobalParams) error OnUnsubscribed(g GlobalParams, topic string) error
// 消息发布事件 // 消息发布事件
OnMessage(GlobalParams) error OnMessage(g GlobalParams, topic string) error
// 保留类型消息事件 // 保留类型消息事件
OnRetainMessage(GlobalParams) error OnRetainMessage(g GlobalParams, topic string) error
// Qos消息完成事件 // Qos消息完成事件
OnQosMessage(GlobalParams) error OnQosMessage(g GlobalParams, topic string) error
// 客户端超时事件 // 客户端超时事件
OnClientExpired(GlobalParams) error OnClientExpired(GlobalParams) error
// 保留消息超时事件 // 保留消息超时事件
OnRetainedExpired(GlobalParams) error OnRetainedExpired(g GlobalParams, topic string) error
} }
type MqttHandleController struct { type MqttHandle struct {
Controller Controller
} }
func (this MqttHandleController) OnSubscribed(g GlobalParams) error { func (this MqttHandle) OnSubscribed(g GlobalParams, topic string) error {
return nil return nil
} }
func (this MqttHandleController) OnUnsubscribed(g GlobalParams) error { func (this MqttHandle) OnUnsubscribed(g GlobalParams, topic string) error {
return nil return nil
} }
func (this MqttHandleController) OnMessage(g GlobalParams) error { func (this MqttHandle) OnMessage(g GlobalParams, topic string) error {
return nil return nil
} }
func (this MqttHandleController) OnRetainMessage(g GlobalParams) error { func (this MqttHandle) OnRetainMessage(g GlobalParams, topic string) error {
return nil return nil
} }
func (this MqttHandleController) OnQosMessage(g GlobalParams) error { func (this MqttHandle) OnQosMessage(g GlobalParams, topic string) error {
return nil return nil
} }
func (this MqttHandleController) OnClientExpired(g GlobalParams) error { func (this MqttHandle) OnClientExpired(g GlobalParams) error {
return nil return nil
} }
func (this MqttHandleController) OnRetainedExpired(g GlobalParams) error { func (this MqttHandle) OnRetainedExpired(g GlobalParams, topic string) error {
return nil return nil
} }
func NewMqttController(container *do.Injector) MqttController { func NewMqttController(container *do.Injector) Mqtt {
return &MqttHandleController{ return &MqttHandle{
Controller: &BaseController{container}, Controller: &BaseController{container},
} }
} }

View File

@ -7,9 +7,11 @@ type Service interface {
RegGRpcs(items map[string]GRpc) error RegGRpcs(items map[string]GRpc) error
RegJobs(items map[string]Job) error RegJobs(items map[string]Job) error
RegCrons(items map[string]Cron) error RegCrons(items map[string]Cron) error
RegMqtts(items map[string]Mqtt) error
GetResListener(code string) (ResListener, bool) GetResListener(code string) (ResListener, bool)
GetAppController(code string) (Controller, bool) GetAppController(code string) (Controller, bool)
GetAppGRpc(code string) (GRpc, bool) GetAppGRpc(code string) (GRpc, bool)
GetAppJob(code string) (Job, bool) GetAppJob(code string) (Job, bool)
GetAppCron(code string) (Cron, bool) GetAppCron(code string) (Cron, bool)
GetAppMqtt(code string) (Mqtt, bool)
} }