41 lines
1.4 KiB
Go
41 lines
1.4 KiB
Go
package nixmsg
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
// transport 抽象 MQTT 应用层通道,便于单测注入假实现。
|
|
type transport interface {
|
|
// Start 开始连接循环(含重连)。凭据在每次 CONNECT 时读取。
|
|
Start(ctx context.Context, cfg transportConfig) error
|
|
// PublishUp 向 up 主题发布一帧。
|
|
PublishUp(payload []byte) error
|
|
// Stop 停止并断开,不再重连。
|
|
Stop(ctx context.Context) error
|
|
// Credential 当前用于 CONNECT 的密码字段(密码或令牌)。
|
|
SetCredential(passwordOrToken string)
|
|
}
|
|
|
|
type transportConfig struct {
|
|
URL string // ws(s):// 或 mqtt(s)://
|
|
EndpointID string
|
|
ConnectTimeout time.Duration
|
|
AllowTCP bool
|
|
Backoff *reconnectBackoff
|
|
OnDown func(payload []byte)
|
|
OnOnline func() // MQTT 已连上且可发(订阅完成前也会调;hello 由上层做)
|
|
OnOffline func()
|
|
OnAuthFailed func(reason AuthReason)
|
|
OnKicked func()
|
|
// OnConnectPacket 每次 CONNECT 构建后回调(单测核对 Clean Start)。
|
|
OnConnectPacket func(cleanStart bool, sessionExpiry uint32)
|
|
// MQTTConnected 在 CONNACK 成功、订阅 down 完成后调用,上层在此发 hello。
|
|
MQTTReady func(ctx context.Context) error
|
|
}
|
|
|
|
// buildCleanConnect 供真实与假传输共用:每次连接 Clean Start=true,会话过期=0。
|
|
func buildCleanConnectFlags() (cleanStart bool, sessionExpiry uint32) {
|
|
return true, 0
|
|
}
|