feat: add WebSocket handler with token auth and session management

This commit is contained in:
2026-03-01 03:03:34 +08:00
parent 35d645a186
commit 39e56e5acc
2 changed files with 201 additions and 0 deletions

41
internal/ws/protocol.go Normal file
View File

@@ -0,0 +1,41 @@
package ws
import "encoding/json"
// ── Client → Server messages ──
// MsgType enumerates known message types.
type MsgType string
const (
MsgStart MsgType = "start" // Begin recording session
MsgStop MsgType = "stop" // End recording session
MsgPaste MsgType = "paste" // Re-paste a history item
)
// ClientMsg is a JSON control message from the phone.
type ClientMsg struct {
Type MsgType `json:"type"`
Text string `json:"text,omitempty"` // Only for "paste"
}
// ── Server → Client messages ──
const (
MsgPartial MsgType = "partial" // Interim ASR result
MsgFinal MsgType = "final" // Final ASR result
MsgPasted MsgType = "pasted" // Paste confirmed
MsgError MsgType = "error" // Error notification
)
// ServerMsg is a JSON message sent to the phone.
type ServerMsg struct {
Type MsgType `json:"type"`
Text string `json:"text,omitempty"`
Message string `json:"message,omitempty"` // For errors
}
func (m ServerMsg) Bytes() []byte {
b, _ := json.Marshal(m)
return b
}