// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package main // hub maintains the set of active connections and broadcasts messages to the // connections. type hub struct { // Registered connections. connections map[*connection]bool // Inbound messages from the connections. broadcast chan []byte // Register requests from the connections. register chan *connection // Unregister requests from connections. unregister chan *connection } var mainHub = hub{ broadcast: make(chan []byte), register: make(chan *connection), unregister: make(chan *connection), connections: make(map[*connection]bool), } func (hub *hub) run() { for { select { case conn := <-hub.register: hub.connections[conn] = true case conn := <-hub.unregister: if _, ok := hub.connections[conn]; ok { delete(hub.connections, conn) close(conn.send) } case message := <-hub.broadcast: for conn := range hub.connections { select { case conn.send <- message: default: close(conn.send) delete(hub.connections, conn) } } } } }