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

@ -4,7 +4,7 @@ import (
"context"
"log"
"git.jinshen.cn/remilia/push-server/interval/server/model"
"git.jinshen.cn/remilia/push-server/interval/protocol"
"github.com/coder/websocket"
)
@ -17,28 +17,28 @@ type Hub struct {
// Invariant:
// - clientsByTopic contains only topics with at least one active subscriber.
// - A topic key must not exist if it has zero clients.
clientsByTopic map[model.Topic]map[string]*Client
clientsByTopic map[protocol.Topic]map[string]*Client
// clientID -> topic -> struct{}
topicsByClients map[string]map[model.Topic]struct{}
topicsByClients map[string]map[protocol.Topic]struct{}
register chan *Client
unregister chan *Client
subscribe chan model.Subscription
unsubscribe chan model.Subscription
broadcast chan model.Message
subscribe chan protocol.Subscription
unsubscribe chan protocol.Subscription
broadcast chan protocol.BroadcastMessage
}
func NewHub() *Hub {
return &Hub{
clientsByID: make(map[string]*Client),
clientsByTopic: make(map[model.Topic]map[string]*Client),
topicsByClients: make(map[string]map[model.Topic]struct{}),
clientsByTopic: make(map[protocol.Topic]map[string]*Client),
topicsByClients: make(map[string]map[protocol.Topic]struct{}),
register: make(chan *Client),
unregister: make(chan *Client),
subscribe: make(chan model.Subscription, 8),
unsubscribe: make(chan model.Subscription, 8),
broadcast: make(chan model.Message, 64),
subscribe: make(chan protocol.Subscription, 8),
unsubscribe: make(chan protocol.Subscription, 8),
broadcast: make(chan protocol.BroadcastMessage, 64),
}
}
@ -50,15 +50,15 @@ func (h *Hub) UnregisterClient(client *Client) {
h.unregister <- client
}
func (h *Hub) Subscribe(sub model.Subscription) {
func (h *Hub) Subscribe(sub protocol.Subscription) {
h.subscribe <- sub
}
func (h *Hub) Unsubscribe(sub model.Subscription) {
func (h *Hub) Unsubscribe(sub protocol.Subscription) {
h.unsubscribe <- sub
}
func (h *Hub) BroadcastMessage(ctx context.Context, msg model.Message) error {
func (h *Hub) BroadcastMessage(ctx context.Context, msg protocol.BroadcastMessage) error {
select {
case h.broadcast <- msg:
return nil
@ -103,7 +103,7 @@ func (h *Hub) getClient(id string) (*Client, bool) {
// Create a new entry for the client in topicsByClients map when it registers.
func (h *Hub) onRegister(c *Client) {
h.clientsByID[c.ID] = c
h.topicsByClients[c.ID] = make(map[model.Topic]struct{})
h.topicsByClients[c.ID] = make(map[protocol.Topic]struct{})
log.Printf("Current clientList: %v\n", h.clientsByID)
}
@ -122,7 +122,7 @@ func (h *Hub) onUnregister(c *Client) {
delete(h.clientsByID, c.ID)
}
func (h *Hub) onSubscribe(s model.Subscription) {
func (h *Hub) onSubscribe(s protocol.Subscription) {
c, ok := h.getClient(s.ClientID)
if !ok {
// If the client does not exist, log an error and return.
@ -135,9 +135,10 @@ func (h *Hub) onSubscribe(s model.Subscription) {
h.clientsByTopic[s.Topic][s.ClientID] = c
h.topicsByClients[s.ClientID][s.Topic] = struct{}{}
log.Printf("Client %s subscribed to topic %s", s.ClientID, s.Topic)
}
func (h *Hub) onUnsubscribe(s model.Subscription) {
func (h *Hub) onUnsubscribe(s protocol.Subscription) {
if clients, ok := h.clientsByTopic[s.Topic]; ok {
delete(clients, s.ClientID)
if len(clients) == 0 {
@ -150,15 +151,16 @@ func (h *Hub) onUnsubscribe(s model.Subscription) {
}
}
func (h *Hub) onBroadcast(msg model.Message) {
func (h *Hub) onBroadcast(msg protocol.BroadcastMessage) {
if !msg.Topic.Valid() {
log.Printf("Broadcast failed: invalid topic")
return
}
log.Printf("Receiving message for topic %s: %s", msg.Topic, string(msg.Content))
log.Printf("Receiving message for topic %s: %s", msg.Topic, string(msg.Payload))
for _, c := range h.clientsByTopic[msg.Topic] {
select {
case c.SendChan <- msg.Content:
case c.SendChan <- msg.Payload:
log.Printf("Sending message to client %s: %s", c.ID, string(msg.Payload))
default:
h.UnregisterClient(c)
if c.Conn != nil {