12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package internal
- // Hub maintains the set of active clients and broadcasts messages to the
- // clients.
- type Hub struct {
- // Registered clients.
- clients map[*Client]bool
- // Inbound messages from the clients.
- broadcast chan []byte
- // Register requests from the clients.
- register chan *Client
- // Unregister requests from clients.
- unregister chan *Client
- }
- func NewHub() *Hub {
- return &Hub{
- broadcast: make(chan []byte),
- register: make(chan *Client),
- unregister: make(chan *Client),
- clients: make(map[*Client]bool),
- }
- }
- func (h *Hub) Run() {
- for {
- select {
- case client := <-h.register:
- h.clients[client] = true
- case client := <-h.unregister:
- if _, ok := h.clients[client]; ok {
- delete(h.clients, client)
- close(client.send)
- }
- case message := <-h.broadcast:
- for client := range h.clients {
- select {
- case client.send <- message:
- default:
- close(client.send)
- delete(h.clients, client)
- }
- }
- }
- }
- }
|