feat: 基本广播服务
- 由Hub接收/push/{topic}的请求并解析信息体广播到对应的Client
This commit is contained in:
39
interval/protocol/message.go
Normal file
39
interval/protocol/message.go
Normal 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")
|
||||
)
|
||||
6
interval/protocol/subscription.go
Normal file
6
interval/protocol/subscription.go
Normal file
@ -0,0 +1,6 @@
|
||||
package protocol
|
||||
|
||||
type Subscription struct {
|
||||
ClientID string
|
||||
Topic Topic
|
||||
}
|
||||
17
interval/protocol/types.go
Normal file
17
interval/protocol/types.go
Normal 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 != ""
|
||||
}
|
||||
Reference in New Issue
Block a user