feat: 实现 Go 与 JS/TS SDK 连接收发与其余接口
This commit is contained in:
+231
@@ -0,0 +1,231 @@
|
||||
package nixmsg
|
||||
|
||||
import "time"
|
||||
|
||||
// ConnectionState 连接状态(onConnection)。
|
||||
type ConnectionState string
|
||||
|
||||
const (
|
||||
StateConnecting ConnectionState = "connecting"
|
||||
StateOnline ConnectionState = "online"
|
||||
StateReconnecting ConnectionState = "reconnecting"
|
||||
StateOffline ConnectionState = "offline"
|
||||
StateKicked ConnectionState = "kicked"
|
||||
StateAuthFailed ConnectionState = "auth_failed"
|
||||
)
|
||||
|
||||
// Credential 登录凭据:密码或会话令牌二选一。
|
||||
type Credential struct {
|
||||
Password string
|
||||
SessionToken string
|
||||
}
|
||||
|
||||
// Options 连接选项。
|
||||
type Options struct {
|
||||
// ManualAck 为 true 时手动确认模式。
|
||||
ManualAck bool
|
||||
// AllowTCP 显式允许裸 TCP(mqtt:// / mqtts://)。默认只用 WebSocket。
|
||||
AllowTCP bool
|
||||
// ConnectTimeout 连接超时,默认 30s。
|
||||
ConnectTimeout time.Duration
|
||||
// ClientLabel hello.client,默认 go-sdk/0.1。
|
||||
ClientLabel string
|
||||
// MaxReceiveBytes hello.max_receive_bytes;0 表示省略(不限)。
|
||||
MaxReceiveBytes int
|
||||
// SendQueueSize 发送队列上限,默认 1000。
|
||||
SendQueueSize int
|
||||
// MaxInflight 在途上限,默认 100。
|
||||
MaxInflight int
|
||||
// DedupCapacity from+id 去重容量,默认 10000。
|
||||
DedupCapacity int
|
||||
// HTTPClient 注册用;nil 用默认。
|
||||
// 测试可注入 transport。
|
||||
transport transport
|
||||
}
|
||||
|
||||
func (o *Options) withDefaults() Options {
|
||||
out := *o
|
||||
if out.ConnectTimeout <= 0 {
|
||||
out.ConnectTimeout = 30 * time.Second
|
||||
}
|
||||
if out.ClientLabel == "" {
|
||||
out.ClientLabel = "go-sdk/0.1"
|
||||
}
|
||||
if out.SendQueueSize <= 0 {
|
||||
out.SendQueueSize = 1000
|
||||
}
|
||||
if out.MaxInflight <= 0 {
|
||||
out.MaxInflight = 100
|
||||
}
|
||||
if out.DedupCapacity <= 0 {
|
||||
out.DedupCapacity = 10000
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Target 发送目标。
|
||||
type Target struct {
|
||||
Kind string `json:"kind"`
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
// Body 消息正文。
|
||||
type Body struct {
|
||||
Enc string `json:"enc"`
|
||||
ContentType string `json:"content_type,omitempty"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
// SendOptions 发送选项。
|
||||
type SendOptions struct {
|
||||
SendAt *time.Time
|
||||
Delay *time.Duration
|
||||
Keep bool
|
||||
TTL *int64
|
||||
Receipt *bool
|
||||
TalkPassword string
|
||||
ContentType string
|
||||
Meta map[string]any
|
||||
ID string // 可选;空则生成 UUIDv7
|
||||
}
|
||||
|
||||
// SendResult 发送结果。
|
||||
type SendResult struct {
|
||||
ID string
|
||||
SendAtMs int64
|
||||
State string
|
||||
}
|
||||
|
||||
// Message 下行消息(交给应用)。
|
||||
type Message struct {
|
||||
ID string
|
||||
From string
|
||||
To Target
|
||||
Body Body
|
||||
Meta map[string]any
|
||||
SendAtMs int64
|
||||
}
|
||||
|
||||
// RecallResult 撤回结果。
|
||||
type RecallResult struct {
|
||||
Result string
|
||||
Recalled int
|
||||
Accepted int
|
||||
Other int
|
||||
}
|
||||
|
||||
// Receipt 回执。
|
||||
type Receipt struct {
|
||||
ReceiptID string
|
||||
ID string
|
||||
EndpointID string
|
||||
State string
|
||||
Reason string
|
||||
AtMs int64
|
||||
}
|
||||
|
||||
// RevokedEvent 撤回/作废事件。
|
||||
type RevokedEvent struct {
|
||||
ID string
|
||||
From string
|
||||
Reason string
|
||||
}
|
||||
|
||||
// PresenceItem 在线查询项。
|
||||
type PresenceItem struct {
|
||||
ID string `json:"id"`
|
||||
Online bool `json:"online"`
|
||||
SinceMs int64 `json:"since_ms"`
|
||||
}
|
||||
|
||||
// DirectoryItem 目录项。
|
||||
type DirectoryItem struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Online bool `json:"online"`
|
||||
OnlineSinceMs int64 `json:"online_since_ms"`
|
||||
OfflineSinceMs int64 `json:"offline_since_ms"`
|
||||
TalkPasswordSet bool `json:"talk_password_set"`
|
||||
}
|
||||
|
||||
// PresenceEvent 上下线通知。
|
||||
type PresenceEvent struct {
|
||||
ID string
|
||||
Online bool
|
||||
AtMs int64
|
||||
}
|
||||
|
||||
// GroupEvent 群事件。
|
||||
type GroupEvent struct {
|
||||
GroupID string
|
||||
Event string
|
||||
EndpointID string
|
||||
AtMs int64
|
||||
}
|
||||
|
||||
// SelfInfo 自己的资料。
|
||||
type SelfInfo struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
DefaultDelayMs int64 `json:"default_delay_ms"`
|
||||
TalkPasswordSet bool `json:"talk_password_set"`
|
||||
}
|
||||
|
||||
// GroupInfo 群摘要。
|
||||
type GroupInfo struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
MemberCount int `json:"member_count"`
|
||||
}
|
||||
|
||||
// GroupMember 群成员。
|
||||
type GroupMember struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Online bool `json:"online"`
|
||||
}
|
||||
|
||||
// GroupMemberIn 建群/加群成员。
|
||||
type GroupMemberIn struct {
|
||||
ID string
|
||||
TalkPassword string
|
||||
}
|
||||
|
||||
// MemberFailure 部分成员失败。
|
||||
type MemberFailure struct {
|
||||
ID string `json:"id"`
|
||||
Code string `json:"code"`
|
||||
}
|
||||
|
||||
// RegisterOptions 注册选项。
|
||||
type RegisterOptions struct {
|
||||
ID string
|
||||
LoginPassword string
|
||||
Name string
|
||||
TalkPassword string
|
||||
}
|
||||
|
||||
// RegisterResult 注册结果。
|
||||
type RegisterResult struct {
|
||||
ID string
|
||||
LoginPassword string // 仅请求未带密码时返回
|
||||
}
|
||||
|
||||
// HandshakeLimits 握手参数。
|
||||
type HandshakeLimits struct {
|
||||
ServerTimeMs int64
|
||||
ServerVersion string
|
||||
MaxBodyBytes int
|
||||
MaxMetaBytes int
|
||||
MaxFrameBytes int
|
||||
MaxTTLSeconds int64
|
||||
MaxScheduleSeconds int64
|
||||
AckTimeoutSeconds int64
|
||||
}
|
||||
|
||||
// ConnectionEvent 连接状态事件。
|
||||
type ConnectionEvent struct {
|
||||
State ConnectionState
|
||||
Reason string // auth_failed / kicked 时的原因
|
||||
}
|
||||
Reference in New Issue
Block a user