feat: 基本广播服务
- 由Hub接收/push/{topic}的请求并解析信息体广播到对应的Client
This commit is contained in:
69
cmd/client/main.go
Normal file
69
cmd/client/main.go
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/coder/websocket"
|
||||||
|
"github.com/coder/websocket/wsjson"
|
||||||
|
|
||||||
|
"git.jinshen.cn/remilia/push-server/interval/protocol"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
log.Println("This is a test client.")
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
c, _, err := websocket.Dial(ctx, "ws://localhost:8080/ws", nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("dial error:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer func() { _ = c.CloseNow() }()
|
||||||
|
|
||||||
|
initMsg := protocol.ControlMessage{
|
||||||
|
Type: protocol.MsgInit,
|
||||||
|
Topics: []protocol.Topic{"news", "sports"},
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Sending init message:", initMsg)
|
||||||
|
|
||||||
|
err = wsjson.Write(ctx, c, initMsg)
|
||||||
|
if err != nil {
|
||||||
|
if websocket.CloseStatus(err) != websocket.StatusNormalClosure {
|
||||||
|
log.Printf("init write failed: %v", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// typ, msg, err := c.Read(ctx)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Println("read error:", err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// switch typ {
|
||||||
|
// case websocket.MessageText:
|
||||||
|
// log.Printf("Received text message: %s", string(msg))
|
||||||
|
// case websocket.MessageBinary:
|
||||||
|
// log.Printf("Received binary message: %v", msg)
|
||||||
|
// }
|
||||||
|
go ReadBroadCastLoop(ctx, c)
|
||||||
|
<-ctx.Done()
|
||||||
|
|
||||||
|
_ = c.Close(websocket.StatusNormalClosure, "test client finished")
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadBroadCastLoop(ctx context.Context, c *websocket.Conn) {
|
||||||
|
for {
|
||||||
|
// var msg protocol.BroadcastMessage
|
||||||
|
var msg []byte
|
||||||
|
if err := wsjson.Read(ctx, c, &msg); err != nil {
|
||||||
|
log.Println("read broadcast error:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Received broadcast message:", string(msg))
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,43 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/coder/websocket"
|
|
||||||
"github.com/coder/websocket/wsjson"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
log.Println("This is a test client.")
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
c, _, err := websocket.Dial(ctx, "ws://localhost:8080/ws", nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal("dial error:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer c.CloseNow()
|
|
||||||
|
|
||||||
err = wsjson.Write(ctx, c, "Hello, WebSocket server!")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal("write error:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
typ, msg, err := c.Read(ctx)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("read error:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
switch typ {
|
|
||||||
case websocket.MessageText:
|
|
||||||
log.Printf("Received text message: %s", string(msg))
|
|
||||||
case websocket.MessageBinary:
|
|
||||||
log.Printf("Received binary message: %v", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Close(websocket.StatusNormalClosure, "test client finished")
|
|
||||||
}
|
|
||||||
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")
|
||||||
|
)
|
||||||
@ -1,6 +1,6 @@
|
|||||||
package model
|
package protocol
|
||||||
|
|
||||||
type Subscription struct {
|
type Subscription struct {
|
||||||
Topic Topic
|
|
||||||
ClientID string
|
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 != ""
|
||||||
|
}
|
||||||
@ -1,2 +0,0 @@
|
|||||||
// Package dto contains data transfer objects used in the interval API.
|
|
||||||
package dto
|
|
||||||
@ -1,17 +0,0 @@
|
|||||||
package dto
|
|
||||||
|
|
||||||
import (
|
|
||||||
"git.jinshen.cn/remilia/push-server/interval/server/model"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Message struct {
|
|
||||||
Topic string `json:"topic"`
|
|
||||||
Content string `json:"content"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func MessageFromModel(m model.Message) Message {
|
|
||||||
return Message{
|
|
||||||
Topic: string(m.Topic),
|
|
||||||
Content: string(m.Content),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,5 +0,0 @@
|
|||||||
package dto
|
|
||||||
|
|
||||||
type PublishRequest struct {
|
|
||||||
Content string `json:"content"`
|
|
||||||
}
|
|
||||||
@ -1,17 +0,0 @@
|
|||||||
package dto
|
|
||||||
|
|
||||||
import (
|
|
||||||
"git.jinshen.cn/remilia/push-server/interval/server/model"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Subscription struct {
|
|
||||||
Topic string `json:"topic"`
|
|
||||||
ClientID string `json:"client_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func SubscriptionFromModel(s model.Subscription) Subscription {
|
|
||||||
return Subscription{
|
|
||||||
Topic: string(s.Topic),
|
|
||||||
ClientID: string(s.ClientID),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -2,25 +2,28 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.jinshen.cn/remilia/push-server/interval/server/api/dto"
|
"git.jinshen.cn/remilia/push-server/interval/protocol"
|
||||||
"git.jinshen.cn/remilia/push-server/interval/server/model"
|
|
||||||
"git.jinshen.cn/remilia/push-server/interval/server/ws"
|
"git.jinshen.cn/remilia/push-server/interval/server/ws"
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type PublishRequest struct {
|
||||||
|
Content string `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
func PushHandler(hub *ws.Hub) http.HandlerFunc {
|
func PushHandler(hub *ws.Hub) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
topicStr := chi.URLParam(r, "topic")
|
topicStr := chi.URLParam(r, "topic")
|
||||||
topic := model.Topic(topicStr)
|
topic := protocol.Topic(topicStr)
|
||||||
if !topic.Valid() {
|
if !topic.Valid() {
|
||||||
http.Error(w, "invalid topic", http.StatusBadRequest)
|
http.Error(w, "invalid topic", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var req dto.PublishRequest
|
var req PublishRequest
|
||||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
http.Error(w, "invalid request body", http.StatusBadRequest)
|
http.Error(w, "invalid request body", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
@ -30,12 +33,14 @@ func PushHandler(hub *ws.Hub) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := model.Message{
|
msg := protocol.BroadcastMessage{
|
||||||
|
Type: protocol.MsgBroadcast,
|
||||||
Topic: topic,
|
Topic: topic,
|
||||||
Content: []byte(req.Content),
|
Payload: json.RawMessage(req.Content),
|
||||||
Timestamp: time.Now().Unix(),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("Received push request for topic %s: %s", topic, req.Content)
|
||||||
|
|
||||||
if err := hub.BroadcastMessage(r.Context(), msg); err != nil {
|
if err := hub.BroadcastMessage(r.Context(), msg); err != nil {
|
||||||
http.Error(w, "request cancelled", http.StatusRequestTimeout)
|
http.Error(w, "request cancelled", http.StatusRequestTimeout)
|
||||||
return
|
return
|
||||||
|
|||||||
@ -1,2 +0,0 @@
|
|||||||
// Package model defines core domain models of the push service.
|
|
||||||
package model
|
|
||||||
@ -1,7 +0,0 @@
|
|||||||
package model
|
|
||||||
|
|
||||||
type Message struct {
|
|
||||||
Topic Topic
|
|
||||||
Content []byte
|
|
||||||
Timestamp int64
|
|
||||||
}
|
|
||||||
@ -1,7 +0,0 @@
|
|||||||
package model
|
|
||||||
|
|
||||||
type Topic string
|
|
||||||
|
|
||||||
func (t Topic) Valid() bool {
|
|
||||||
return t != ""
|
|
||||||
}
|
|
||||||
@ -2,10 +2,12 @@ package ws
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
|
||||||
|
|
||||||
|
"git.jinshen.cn/remilia/push-server/interval/protocol"
|
||||||
"github.com/coder/websocket"
|
"github.com/coder/websocket"
|
||||||
|
"github.com/coder/websocket/wsjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Client represents a connected client in the hub.
|
// Client represents a connected client in the hub.
|
||||||
@ -13,28 +15,58 @@ type Client struct {
|
|||||||
ID string
|
ID string
|
||||||
Conn *websocket.Conn
|
Conn *websocket.Conn
|
||||||
SendChan chan []byte
|
SendChan chan []byte
|
||||||
|
Hub *Hub
|
||||||
|
|
||||||
Ctx context.Context
|
Ctx context.Context
|
||||||
Cancel context.CancelFunc
|
Cancel context.CancelFunc
|
||||||
|
|
||||||
|
inited bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(id string, conn *websocket.Conn, parentCtx context.Context) *Client {
|
func NewClient(id string, conn *websocket.Conn, hub *Hub, parentCtx context.Context) *Client {
|
||||||
ctx, cancel := context.WithCancel(parentCtx)
|
ctx, cancel := context.WithCancel(parentCtx)
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
ID: id,
|
ID: id,
|
||||||
Conn: conn,
|
Conn: conn,
|
||||||
SendChan: make(chan []byte, 32),
|
SendChan: make(chan []byte, 32),
|
||||||
|
Hub: hub,
|
||||||
|
|
||||||
Ctx: ctx,
|
Ctx: ctx,
|
||||||
Cancel: cancel,
|
Cancel: cancel,
|
||||||
|
|
||||||
|
inited: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (c *Client) ReadLoop() {
|
||||||
|
defer c.Close()
|
||||||
|
|
||||||
|
for {
|
||||||
|
var msg protocol.ControlMessage
|
||||||
|
|
||||||
|
err := wsjson.Read(c.Ctx, c.Conn, &msg)
|
||||||
|
if err != nil {
|
||||||
|
if websocket.CloseStatus(err) == websocket.StatusNormalClosure {
|
||||||
|
log.Println("WebSocket closed normally:", err)
|
||||||
|
} else {
|
||||||
|
log.Println("WebSocket read error:", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := msg.Validate(); err != nil {
|
||||||
|
_ = c.Conn.Close(websocket.StatusPolicyViolation, "invalid message")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.handleControlMessage(msg); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write message to websocket connection.
|
func (c *Client) WriteLoop() {
|
||||||
func (c *Client) WriteMessage() {
|
defer c.Close()
|
||||||
defer func() {
|
|
||||||
_ = c.Conn.Close(websocket.StatusNormalClosure, "client writer closed")
|
|
||||||
}()
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@ -44,11 +76,7 @@ func (c *Client) WriteMessage() {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
err := wsjson.Write(c.Ctx, c.Conn, msg)
|
||||||
writeCtx, cancel := context.WithTimeout(c.Ctx, 5*time.Second)
|
|
||||||
err := c.Conn.Write(writeCtx, websocket.MessageText, msg)
|
|
||||||
cancel()
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("WebSocket write error:", err)
|
log.Println("WebSocket write error:", err)
|
||||||
return
|
return
|
||||||
@ -56,3 +84,40 @@ func (c *Client) WriteMessage() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) handleControlMessage(msg protocol.ControlMessage) error {
|
||||||
|
switch msg.Type {
|
||||||
|
case protocol.MsgInit:
|
||||||
|
if c.inited {
|
||||||
|
return errors.New("already initialized")
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Client %s initializing with topics: %v", c.ID, msg.Topics)
|
||||||
|
c.Hub.RegisterClient(c)
|
||||||
|
|
||||||
|
for _, t := range msg.Topics {
|
||||||
|
c.Hub.Subscribe(protocol.Subscription{
|
||||||
|
ClientID: c.ID,
|
||||||
|
Topic: protocol.Topic(t),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
c.inited = true
|
||||||
|
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
if !c.inited {
|
||||||
|
return errors.New("client not initialized")
|
||||||
|
}
|
||||||
|
log.Println("Unknown control message type:", msg.Type)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Close() {
|
||||||
|
c.Cancel()
|
||||||
|
c.Hub.UnregisterClient(c)
|
||||||
|
if c.Conn != nil {
|
||||||
|
_ = c.Conn.Close(websocket.StatusNormalClosure, "client closed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -2,7 +2,6 @@ package ws
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
@ -23,53 +22,11 @@ func Handler(ctx context.Context, h *Hub) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c := NewClient(r.RemoteAddr, conn, ctx)
|
c := NewClient(r.RemoteAddr, conn, h, ctx)
|
||||||
log.Println("Client", r.RemoteAddr, "connected.")
|
log.Println("Client", r.RemoteAddr, "connected.")
|
||||||
h.RegisterClient(c)
|
|
||||||
|
|
||||||
go echo(c, h)
|
go c.ReadLoop()
|
||||||
|
go c.WriteLoop()
|
||||||
go heartbeat(c)
|
go heartbeat(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func echo(c *Client, h *Hub) {
|
|
||||||
defer func() {
|
|
||||||
if c.Conn != nil {
|
|
||||||
log.Println("Closing WebSocket connection")
|
|
||||||
h.UnregisterClient(c)
|
|
||||||
c.Cancel()
|
|
||||||
_ = c.Conn.Close(websocket.StatusNormalClosure, "echo finished")
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
for {
|
|
||||||
typ, r, err := c.Conn.Reader(c.Ctx)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
if websocket.CloseStatus(err) == websocket.StatusNormalClosure {
|
|
||||||
log.Println("WebSocket connection closed normally")
|
|
||||||
} else {
|
|
||||||
log.Println("WebSocket reader error:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w, err := c.Conn.Writer(c.Ctx, typ)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("WebSocket writer error:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = io.Copy(w, r)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("WebSocket copy error:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = w.Close(); err != nil {
|
|
||||||
log.Println("WebSocket writer close error:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"git.jinshen.cn/remilia/push-server/interval/server/model"
|
"git.jinshen.cn/remilia/push-server/interval/protocol"
|
||||||
"github.com/coder/websocket"
|
"github.com/coder/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -17,28 +17,28 @@ type Hub struct {
|
|||||||
// Invariant:
|
// Invariant:
|
||||||
// - clientsByTopic contains only topics with at least one active subscriber.
|
// - clientsByTopic contains only topics with at least one active subscriber.
|
||||||
// - A topic key must not exist if it has zero clients.
|
// - 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{}
|
// clientID -> topic -> struct{}
|
||||||
topicsByClients map[string]map[model.Topic]struct{}
|
topicsByClients map[string]map[protocol.Topic]struct{}
|
||||||
|
|
||||||
register chan *Client
|
register chan *Client
|
||||||
unregister chan *Client
|
unregister chan *Client
|
||||||
subscribe chan model.Subscription
|
subscribe chan protocol.Subscription
|
||||||
unsubscribe chan model.Subscription
|
unsubscribe chan protocol.Subscription
|
||||||
broadcast chan model.Message
|
broadcast chan protocol.BroadcastMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHub() *Hub {
|
func NewHub() *Hub {
|
||||||
return &Hub{
|
return &Hub{
|
||||||
clientsByID: make(map[string]*Client),
|
clientsByID: make(map[string]*Client),
|
||||||
clientsByTopic: make(map[model.Topic]map[string]*Client),
|
clientsByTopic: make(map[protocol.Topic]map[string]*Client),
|
||||||
topicsByClients: make(map[string]map[model.Topic]struct{}),
|
topicsByClients: make(map[string]map[protocol.Topic]struct{}),
|
||||||
|
|
||||||
register: make(chan *Client),
|
register: make(chan *Client),
|
||||||
unregister: make(chan *Client),
|
unregister: make(chan *Client),
|
||||||
subscribe: make(chan model.Subscription, 8),
|
subscribe: make(chan protocol.Subscription, 8),
|
||||||
unsubscribe: make(chan model.Subscription, 8),
|
unsubscribe: make(chan protocol.Subscription, 8),
|
||||||
broadcast: make(chan model.Message, 64),
|
broadcast: make(chan protocol.BroadcastMessage, 64),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,15 +50,15 @@ func (h *Hub) UnregisterClient(client *Client) {
|
|||||||
h.unregister <- client
|
h.unregister <- client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hub) Subscribe(sub model.Subscription) {
|
func (h *Hub) Subscribe(sub protocol.Subscription) {
|
||||||
h.subscribe <- sub
|
h.subscribe <- sub
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hub) Unsubscribe(sub model.Subscription) {
|
func (h *Hub) Unsubscribe(sub protocol.Subscription) {
|
||||||
h.unsubscribe <- sub
|
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 {
|
select {
|
||||||
case h.broadcast <- msg:
|
case h.broadcast <- msg:
|
||||||
return nil
|
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.
|
// Create a new entry for the client in topicsByClients map when it registers.
|
||||||
func (h *Hub) onRegister(c *Client) {
|
func (h *Hub) onRegister(c *Client) {
|
||||||
h.clientsByID[c.ID] = c
|
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)
|
log.Printf("Current clientList: %v\n", h.clientsByID)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,7 +122,7 @@ func (h *Hub) onUnregister(c *Client) {
|
|||||||
delete(h.clientsByID, c.ID)
|
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)
|
c, ok := h.getClient(s.ClientID)
|
||||||
if !ok {
|
if !ok {
|
||||||
// If the client does not exist, log an error and return.
|
// 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.clientsByTopic[s.Topic][s.ClientID] = c
|
||||||
h.topicsByClients[s.ClientID][s.Topic] = struct{}{}
|
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 {
|
if clients, ok := h.clientsByTopic[s.Topic]; ok {
|
||||||
delete(clients, s.ClientID)
|
delete(clients, s.ClientID)
|
||||||
if len(clients) == 0 {
|
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() {
|
if !msg.Topic.Valid() {
|
||||||
log.Printf("Broadcast failed: invalid topic")
|
log.Printf("Broadcast failed: invalid topic")
|
||||||
return
|
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] {
|
for _, c := range h.clientsByTopic[msg.Topic] {
|
||||||
select {
|
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:
|
default:
|
||||||
h.UnregisterClient(c)
|
h.UnregisterClient(c)
|
||||||
if c.Conn != nil {
|
if c.Conn != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user