feat: 基本广播服务

- 由Hub接收/push/{topic}的请求并解析信息体广播到对应的Client
This commit is contained in:
2025-12-17 14:49:59 +08:00
parent 53555a31c0
commit 1dbcc03e46
16 changed files with 245 additions and 191 deletions

View File

@ -0,0 +1,39 @@
package protocol
import (
"encoding/json"
"errors"
)
type ControlMessage struct {
Type MessageType `json:"type"`
Topic Topic `json:"topic,omitempty"`
Topics []Topic `json:"topics,omitempty"`
}
type BroadcastMessage struct {
Type MessageType `json:"type"`
Topic Topic `json:"topic"`
Payload json.RawMessage `json:"payload"`
}
func (m ControlMessage) Validate() error {
switch m.Type {
case MsgInit:
if len(m.Topics) == 0 {
return errors.New("init requires topics")
}
case MsgSubscribe:
if m.Topic == "" {
return errors.New("subscribe requires topic")
}
default:
return errors.New("unknown message type")
}
return nil
}
var (
ErrInvalidMessage = errors.New("invalid message")
ErrPolicyViolation = errors.New("policy violation")
)

View File

@ -0,0 +1,6 @@
package protocol
type Subscription struct {
ClientID string
Topic Topic
}

View File

@ -0,0 +1,17 @@
package protocol
type MessageType string
const (
MsgInit MessageType = "init"
MsgSubscribe MessageType = "subscribe"
MsgUnsubscribe MessageType = "unsubscribe"
MsgBroadcast MessageType = "broadcast"
MsgError MessageType = "error"
)
type Topic string
func (t Topic) Valid() bool {
return t != ""
}