Files
Aroll-Cutter/ws/hub.go
2026-02-25 00:27:36 -08:00

58 lines
1.1 KiB
Go

package ws
// Client represents a single WebSocket connection.
// The handler owns the actual *websocket.Conn; the hub only needs the send channel.
type Client struct {
Send chan []byte
}
// Hub manages all active clients and routes broadcast messages to them.
type Hub struct {
clients map[*Client]struct{}
Register chan *Client
Unregister chan *Client
broadcast chan []byte
}
func NewHub() *Hub {
return &Hub{
clients: make(map[*Client]struct{}),
Register: make(chan *Client, 8),
Unregister: make(chan *Client, 8),
broadcast: make(chan []byte, 512),
}
}
func (h *Hub) Run() {
for {
select {
case c := <-h.Register:
h.clients[c] = struct{}{}
case c := <-h.Unregister:
if _, ok := h.clients[c]; ok {
delete(h.clients, c)
close(c.Send)
}
case msg := <-h.broadcast:
for c := range h.clients {
select {
case c.Send <- msg:
default:
delete(h.clients, c)
close(c.Send)
}
}
}
}
}
// Broadcast sends msg to every connected client (non-blocking).
func (h *Hub) Broadcast(msg []byte) {
select {
case h.broadcast <- msg:
default:
}
}