33 lines
875 B
Go
33 lines
875 B
Go
package server
|
|
|
|
// Hub is the central registry/bus for all connected clients.
|
|
//
|
|
// It exposes channels for broadcast, register, and unregister events
|
|
// and protects the Clients map with a RWMutex for safe concurrent access.
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
// Hub groups clients and channels used by the server runtime.
|
|
type Hub struct {
|
|
Clients map[*Client]bool
|
|
Broadcast chan []byte
|
|
Register chan *Client
|
|
Unregister chan *Client
|
|
Mutex sync.RWMutex
|
|
Upgrader websocket.Upgrader
|
|
}
|
|
|
|
// NewHub constructs a Hub with buffered channels sized by bufferSize.
|
|
func NewHub(bufferSize int, upgrader websocket.Upgrader) *Hub {
|
|
return &Hub{
|
|
Clients: make(map[*Client]bool),
|
|
Broadcast: make(chan []byte, bufferSize),
|
|
Register: make(chan *Client, bufferSize),
|
|
Unregister: make(chan *Client, bufferSize),
|
|
Upgrader: upgrader,
|
|
}
|
|
}
|