Files
gochat/backend/internal/handler/ws/protocol.go
T
rogee 0dabb8cfa5 docs: 整理文档目录结构 — 清理过时文档、归集功能子目录、统一命名规范
清理:
- 删除 34 份过时文档(gap reports/QA临时报告/验收报告/阶段性文档)
- 删除 docs/.hermes/skills 第三方 skills 副本(16 文件)
- 删除 skills-lock.json

目录归集:
- 根目录仅保留 README.md 索引
- product/ — 产品与架构设计(PRD + ARCHITECTURE + P2设计文档 + AI/企业路线图)
- tracking/ — Chatwoot parity 开发跟踪
- requirements/ — M01-M12 模块需求
- plans/ — 历史实现计划
- parity/ — 路由 parity 与前端契约
- qa/ — QA 报告与测试计划
- ops/ — 运维部署

命名规范:
- 全小写 kebab-case,禁止全大写文件名
- product/tracking/ops 用 NN- 序号前缀
- requirements 用 MNN- 两位零填充模块号
- plans/qa 用 YYYY-MM-DD- 日期前缀
- requirements M1-M9 零填充为 M01-M09(修复字典序)

同步更新:
- backend/cmd/route_parity/main.go 路径默认值
- backend/scripts/parity_frontend_smoke.sh 报告路径
- 所有 docs 内部交叉引用
- .gitignore 排除编译产物 (backend/gochat, backend/route_parity)
- 新增迁移 000052/000053
- 前端 WS 相关修改
2026-07-09 14:53:27 +08:00

165 lines
6.1 KiB
Go

package ws
// Protocol defines the WebSocket message frame format and event type constants.
// Reference: Chatwoot ActionCable protocol — subscribe/command/message pattern.
//
// Client → Server commands:
// {"command":"subscribe","identifier":"{\"channel\":\"AccountChannel\",\"account_id\":1}"}}
// {"command":"unsubscribe","identifier":"{\"channel\":\"AccountChannel\",\"account_id\":1}"}}
// {"command":"ping"}
//
// Server → Client events:
// {"type":"event","event":"message.created","payload":{...},"identifier":"{\"channel\":\"AccountChannel\",\"account_id\":1}"}
// {"type":"confirm_subscribe","identifier":"..."}
// {"type":"confirm_unsubscribe","identifier":"..."}
// {"type":"ping","message":"2026-05-23T10:00:00Z"}
// {"type":"reject_subscribe","identifier":"...","reason":"..."}
// CommandType — client→server action types
type CommandType string
const (
CommandSubscribe CommandType = "subscribe"
CommandUnsubscribe CommandType = "unsubscribe"
CommandPing CommandType = "ping"
CommandMessage CommandType = "message" // client→channel action (e.g. update_presence)
)
// ServerMessageType — server→client message types
type ServerMessageType string
const (
// ServerEvent pushes a real-time event to subscribed clients
ServerEvent ServerMessageType = "event"
// ServerConfirmSubscribe acknowledges a successful subscription
ServerConfirmSubscribe ServerMessageType = "confirm_subscription"
// ServerConfirmUnsubscribe acknowledges a successful unsubscribe
ServerConfirmUnsubscribe ServerMessageType = "confirm_unsubscribe" // NOTE: ActionCable uses confirm_subscription for both sub and unsub
// ServerRejectSubscribe rejects a subscription attempt
ServerRejectSubscribe ServerMessageType = "reject_subscription"
// ServerPing is a heartbeat response
ServerPing ServerMessageType = "ping"
// ServerWelcome is sent immediately upon connection
ServerWelcome ServerMessageType = "welcome"
// ServerDisconnect is sent before closing the connection
ServerDisconnect ServerMessageType = "disconnect"
)
// --- Client → Server Frames ---
// CommandFrame is the frame clients send to the server.
// Mirrors Chatwoot ActionCable's command structure.
type CommandFrame struct {
Command CommandType `json:"command"`
Identifier string `json:"identifier"` // JSON-encoded ChannelIdentifier
Data string `json:"data,omitempty"` // optional action data
}
// ChannelIdentifier describes which "channel" (room) the client wants to subscribe to.
// Serialized as JSON string in the `identifier` field, matching ActionCable convention.
type ChannelIdentifier struct {
Channel string `json:"channel"` // "AccountChannel" or "ConversationChannel"
AccountID uint `json:"account_id"` // required for both channels
ConversationID uint `json:"conversation_id,omitempty"` // required for ConversationChannel
}
// Channel name constants (ActionCable naming style)
const (
ChannelAccount = "AccountChannel"
ChannelConversation = "ConversationChannel"
ChannelRoom = "RoomChannel" // Chatwoot single-subscription channel
)
// --- Server → Client Frames ---
// EventFrame pushes a real-time event payload to the client.
type EventFrame struct {
Type ServerMessageType `json:"type"`
Event string `json:"event,omitempty"` // e.g. "message.created"
Payload interface{} `json:"payload,omitempty"` // event data
Identifier string `json:"identifier,omitempty"` // channel identifier
}
// ConfirmFrame acknowledges a subscribe/unsubscribe command.
type ConfirmFrame struct {
Type ServerMessageType `json:"type"`
Identifier string `json:"identifier"`
}
// RejectFrame rejects a subscribe command with a reason.
type RejectFrame struct {
Type ServerMessageType `json:"type"`
Identifier string `json:"identifier"`
Reason string `json:"reason"`
}
// PingFrame is a heartbeat pong response.
type PingFrame struct {
Type ServerMessageType `json:"type"`
Message string `json:"message"` // timestamp string
}
// WelcomeFrame is sent upon successful WebSocket connection.
type WelcomeFrame struct {
Type ServerMessageType `json:"type"`
}
// DisconnectFrame is sent before closing a connection.
type DisconnectFrame struct {
Type ServerMessageType `json:"type"`
Reason string `json:"reason"`
Reconnect bool `json:"reconnect"`
}
// --- Real-time Event Type Constants ---
// These match the Watermill PubSub topic names and channel.EventType values.
const (
// Message events
EventMessageCreated = "message.created"
EventMessageUpdated = "message.updated"
EventMessageDeleted = "message.deleted"
// Conversation events
EventConversationCreated = "conversation.created"
EventConversationUpdated = "conversation.updated"
EventConversationResolved = "conversation.resolved"
EventConversationOpened = "conversation.opened"
EventConversationAssigned = "conversation.assigned"
EventConversationUnassigned = "conversation.unassigned"
// Contact events
EventContactCreated = "contact.created"
EventContactUpdated = "contact.updated"
EventContactDeleted = "contact.deleted"
// Agent/typing events
EventAgentTypingOn = "agent.typing_on"
EventAgentTypingOff = "agent.typing_off"
EventAgentOnline = "agent.online"
EventAgentOffline = "agent.offline"
// Inbox events
EventInboxCreated = "inbox.created"
EventInboxUpdated = "inbox.updated"
EventInboxDeleted = "inbox.deleted"
// System notification event
EventSystemNotification = "system.notification"
// P4 M8 — Notification+Webhook event types
EventNotificationCreated = "notification.created"
EventNotificationUpdated = "notification.updated"
EventNotificationDeleted = "notification.deleted"
// Account cache event types
EventAccountCacheInvalidated = "account.cache_invalidated"
)
// --- Ping/pong Configuration ---
const (
// PingInterval is how often the server sends ping frames to detect dead connections.
PingInterval = 5 // seconds — must be < ActionCable staleThreshold (6s) to avoid reconnect loops
)