diff --git a/.gitignore b/.gitignore index 0026861..ac71020 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,6 @@ _cgo_export.* _testmain.go *.exe + +.idea/ +*.iml \ No newline at end of file diff --git a/examples/chat/conn.go b/examples/chat/conn.go index 40fd38c..04e4707 100644 --- a/examples/chat/conn.go +++ b/examples/chat/conn.go @@ -42,7 +42,7 @@ type connection struct { // readPump pumps messages from the websocket connection to the hub. func (c *connection) readPump() { defer func() { - h.unregister <- c + mainHub.unregister <- c c.ws.Close() }() c.ws.SetReadLimit(maxMessageSize) @@ -56,7 +56,7 @@ func (c *connection) readPump() { } break } - h.broadcast <- message + mainHub.broadcast <- message } } @@ -99,7 +99,7 @@ func serveWs(w http.ResponseWriter, r *http.Request) { return } c := &connection{send: make(chan []byte, 256), ws: ws} - h.register <- c + mainHub.register <- c go c.writePump() c.readPump() } diff --git a/examples/chat/hub.go b/examples/chat/hub.go index 449ba75..c94c3d4 100644 --- a/examples/chat/hub.go +++ b/examples/chat/hub.go @@ -20,30 +20,30 @@ type hub struct { unregister chan *connection } -var h = hub{ +var mainHub = hub{ broadcast: make(chan []byte), register: make(chan *connection), unregister: make(chan *connection), connections: make(map[*connection]bool), } -func (h *hub) run() { +func (hub *hub) run() { for { select { - case c := <-h.register: - h.connections[c] = true - case c := <-h.unregister: - if _, ok := h.connections[c]; ok { - delete(h.connections, c) + case c := <-hub.register: + hub.connections[c] = true + case c := <-hub.unregister: + if _, ok := hub.connections[c]; ok { + delete(hub.connections, c) close(c.send) } - case m := <-h.broadcast: - for c := range h.connections { + case m := <-hub.broadcast: + for c := range hub.connections { select { case c.send <- m: default: close(c.send) - delete(h.connections, c) + delete(hub.connections, c) } } } diff --git a/examples/chat/main.go b/examples/chat/main.go index 3c4448d..14b2e99 100644 --- a/examples/chat/main.go +++ b/examples/chat/main.go @@ -12,7 +12,7 @@ import ( ) var addr = flag.String("addr", ":8080", "http service address") -var homeTempl = template.Must(template.ParseFiles("home.html")) +var homeTemplate = template.Must(template.ParseFiles("home.html")) func serveHome(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { @@ -24,12 +24,12 @@ func serveHome(w http.ResponseWriter, r *http.Request) { return } w.Header().Set("Content-Type", "text/html; charset=utf-8") - homeTempl.Execute(w, r.Host) + homeTemplate.Execute(w, r.Host) } func main() { flag.Parse() - go h.run() + go mainHub.run() http.HandleFunc("/", serveHome) http.HandleFunc("/ws", serveWs) err := http.ListenAndServe(*addr, nil)