From a6108176e8b811b74d45ab67163a03d6c527c0c5 Mon Sep 17 00:00:00 2001 From: Jean de Klerk Date: Wed, 18 May 2016 08:56:16 -0600 Subject: [PATCH 1/3] Meaningful names for hub --- .gitignore | 3 +++ examples/chat/conn.go | 6 +++--- examples/chat/hub.go | 20 ++++++++++---------- examples/chat/main.go | 6 +++--- 4 files changed, 19 insertions(+), 16 deletions(-) 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) From be95f72b73315542abb81bf8dbde9ca5b414cef9 Mon Sep 17 00:00:00 2001 From: Jean de Klerk Date: Wed, 18 May 2016 08:57:16 -0600 Subject: [PATCH 2/3] Meaningful names for connection, message --- examples/chat/conn.go | 40 ++++++++++++++++++++-------------------- examples/chat/hub.go | 22 +++++++++++----------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/examples/chat/conn.go b/examples/chat/conn.go index 04e4707..6d7dd27 100644 --- a/examples/chat/conn.go +++ b/examples/chat/conn.go @@ -40,16 +40,16 @@ type connection struct { } // readPump pumps messages from the websocket connection to the hub. -func (c *connection) readPump() { +func (conn *connection) readPump() { defer func() { - mainHub.unregister <- c - c.ws.Close() + mainHub.unregister <- conn + conn.ws.Close() }() - c.ws.SetReadLimit(maxMessageSize) - c.ws.SetReadDeadline(time.Now().Add(pongWait)) - c.ws.SetPongHandler(func(string) error { c.ws.SetReadDeadline(time.Now().Add(pongWait)); return nil }) + conn.ws.SetReadLimit(maxMessageSize) + conn.ws.SetReadDeadline(time.Now().Add(pongWait)) + conn.ws.SetPongHandler(func(string) error { conn.ws.SetReadDeadline(time.Now().Add(pongWait)); return nil }) for { - _, message, err := c.ws.ReadMessage() + _, message, err := conn.ws.ReadMessage() if err != nil { if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) { log.Printf("error: %v", err) @@ -61,30 +61,30 @@ func (c *connection) readPump() { } // write writes a message with the given message type and payload. -func (c *connection) write(mt int, payload []byte) error { - c.ws.SetWriteDeadline(time.Now().Add(writeWait)) - return c.ws.WriteMessage(mt, payload) +func (conn *connection) write(mt int, payload []byte) error { + conn.ws.SetWriteDeadline(time.Now().Add(writeWait)) + return conn.ws.WriteMessage(mt, payload) } // writePump pumps messages from the hub to the websocket connection. -func (c *connection) writePump() { +func (conn *connection) writePump() { ticker := time.NewTicker(pingPeriod) defer func() { ticker.Stop() - c.ws.Close() + conn.ws.Close() }() for { select { - case message, ok := <-c.send: + case message, ok := <-conn.send: if !ok { - c.write(websocket.CloseMessage, []byte{}) + conn.write(websocket.CloseMessage, []byte{}) return } - if err := c.write(websocket.TextMessage, message); err != nil { + if err := conn.write(websocket.TextMessage, message); err != nil { return } case <-ticker.C: - if err := c.write(websocket.PingMessage, []byte{}); err != nil { + if err := conn.write(websocket.PingMessage, []byte{}); err != nil { return } } @@ -98,8 +98,8 @@ func serveWs(w http.ResponseWriter, r *http.Request) { log.Println(err) return } - c := &connection{send: make(chan []byte, 256), ws: ws} - mainHub.register <- c - go c.writePump() - c.readPump() + conn := &connection{send: make(chan []byte, 256), ws: ws} + mainHub.register <- conn + go conn.writePump() + conn.readPump() } diff --git a/examples/chat/hub.go b/examples/chat/hub.go index c94c3d4..baa00e1 100644 --- a/examples/chat/hub.go +++ b/examples/chat/hub.go @@ -30,20 +30,20 @@ var mainHub = hub{ func (hub *hub) run() { for { select { - 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 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 m := <-hub.broadcast: - for c := range hub.connections { + case message := <-hub.broadcast: + for conn := range hub.connections { select { - case c.send <- m: + case conn.send <- message: default: - close(c.send) - delete(hub.connections, c) + close(conn.send) + delete(hub.connections, conn) } } } From 24cddddcc013c647610efaf5764032a832eb5bc4 Mon Sep 17 00:00:00 2001 From: Gary Burd Date: Thu, 26 May 2016 21:00:24 -0700 Subject: [PATCH 3/3] Use single character receiver names in chat example --- examples/chat/conn.go | 42 +++++++++++++++++++++--------------------- examples/chat/hub.go | 32 ++++++++++++++++---------------- examples/chat/main.go | 2 +- 3 files changed, 38 insertions(+), 38 deletions(-) diff --git a/examples/chat/conn.go b/examples/chat/conn.go index 6d7dd27..2fe2357 100644 --- a/examples/chat/conn.go +++ b/examples/chat/conn.go @@ -30,8 +30,8 @@ var upgrader = websocket.Upgrader{ WriteBufferSize: 1024, } -// connection is an middleman between the websocket connection and the hub. -type connection struct { +// Conn is an middleman between the websocket connection and the hub. +type Conn struct { // The websocket connection. ws *websocket.Conn @@ -40,51 +40,51 @@ type connection struct { } // readPump pumps messages from the websocket connection to the hub. -func (conn *connection) readPump() { +func (c *Conn) readPump() { defer func() { - mainHub.unregister <- conn - conn.ws.Close() + hub.unregister <- c + c.ws.Close() }() - conn.ws.SetReadLimit(maxMessageSize) - conn.ws.SetReadDeadline(time.Now().Add(pongWait)) - conn.ws.SetPongHandler(func(string) error { conn.ws.SetReadDeadline(time.Now().Add(pongWait)); return nil }) + c.ws.SetReadLimit(maxMessageSize) + c.ws.SetReadDeadline(time.Now().Add(pongWait)) + c.ws.SetPongHandler(func(string) error { c.ws.SetReadDeadline(time.Now().Add(pongWait)); return nil }) for { - _, message, err := conn.ws.ReadMessage() + _, message, err := c.ws.ReadMessage() if err != nil { if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) { log.Printf("error: %v", err) } break } - mainHub.broadcast <- message + hub.broadcast <- message } } // write writes a message with the given message type and payload. -func (conn *connection) write(mt int, payload []byte) error { - conn.ws.SetWriteDeadline(time.Now().Add(writeWait)) - return conn.ws.WriteMessage(mt, payload) +func (c *Conn) write(mt int, payload []byte) error { + c.ws.SetWriteDeadline(time.Now().Add(writeWait)) + return c.ws.WriteMessage(mt, payload) } // writePump pumps messages from the hub to the websocket connection. -func (conn *connection) writePump() { +func (c *Conn) writePump() { ticker := time.NewTicker(pingPeriod) defer func() { ticker.Stop() - conn.ws.Close() + c.ws.Close() }() for { select { - case message, ok := <-conn.send: + case message, ok := <-c.send: if !ok { - conn.write(websocket.CloseMessage, []byte{}) + c.write(websocket.CloseMessage, []byte{}) return } - if err := conn.write(websocket.TextMessage, message); err != nil { + if err := c.write(websocket.TextMessage, message); err != nil { return } case <-ticker.C: - if err := conn.write(websocket.PingMessage, []byte{}); err != nil { + if err := c.write(websocket.PingMessage, []byte{}); err != nil { return } } @@ -98,8 +98,8 @@ func serveWs(w http.ResponseWriter, r *http.Request) { log.Println(err) return } - conn := &connection{send: make(chan []byte, 256), ws: ws} - mainHub.register <- conn + conn := &Conn{send: make(chan []byte, 256), ws: ws} + hub.register <- conn go conn.writePump() conn.readPump() } diff --git a/examples/chat/hub.go b/examples/chat/hub.go index baa00e1..92da865 100644 --- a/examples/chat/hub.go +++ b/examples/chat/hub.go @@ -6,39 +6,39 @@ package main // hub maintains the set of active connections and broadcasts messages to the // connections. -type hub struct { +type Hub struct { // Registered connections. - connections map[*connection]bool + connections map[*Conn]bool // Inbound messages from the connections. broadcast chan []byte // Register requests from the connections. - register chan *connection + register chan *Conn // Unregister requests from connections. - unregister chan *connection + unregister chan *Conn } -var mainHub = hub{ +var hub = Hub{ broadcast: make(chan []byte), - register: make(chan *connection), - unregister: make(chan *connection), - connections: make(map[*connection]bool), + register: make(chan *Conn), + unregister: make(chan *Conn), + connections: make(map[*Conn]bool), } -func (hub *hub) run() { +func (h *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) + case conn := <-h.register: + h.connections[conn] = true + case conn := <-h.unregister: + if _, ok := h.connections[conn]; ok { + delete(h.connections, conn) close(conn.send) } - case message := <-hub.broadcast: - for conn := range hub.connections { + case message := <-h.broadcast: + for conn := range h.connections { select { case conn.send <- message: default: diff --git a/examples/chat/main.go b/examples/chat/main.go index 14b2e99..39943e6 100644 --- a/examples/chat/main.go +++ b/examples/chat/main.go @@ -29,7 +29,7 @@ func serveHome(w http.ResponseWriter, r *http.Request) { func main() { flag.Parse() - go mainHub.run() + go hub.run() http.HandleFunc("/", serveHome) http.HandleFunc("/ws", serveWs) err := http.ListenAndServe(*addr, nil)