feat: Hub接收RESTful API的消息
This commit is contained in:
@ -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:
|
||||
|
||||
Reference in New Issue
Block a user