feat: Hub接收RESTful API的消息

This commit is contained in:
2025-12-16 17:15:59 +08:00
parent 18874711ea
commit b824dc3792
6 changed files with 74 additions and 7 deletions

View File

@ -65,11 +65,18 @@ func (h *Hub) Unsubscribe(sub model.Subscription) {
h.unsubscribe <- sub
}
func (h *Hub) BroadcastMessage(message model.Message) {
h.broadcast <- message
func (h *Hub) BroadcastMessage(ctx context.Context, msg model.Message) error {
select {
case h.broadcast <- msg:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (h *Hub) Run(ctx context.Context) {
log.Println("Hub is running")
for {
select {
case c := <-h.register:
@ -155,6 +162,7 @@ func (h *Hub) onBroadcast(msg model.Message) {
log.Printf("Broadcast failed: invalid topic")
return
}
log.Printf("Receiving message for topic %s: %s", msg.Topic, string(msg.Content))
for _, c := range h.clientsByTopic[msg.Topic] {
select {
case c.SendChan <- msg.Content: