Merge branch 'varnames'

This commit is contained in:
Gary Burd 2016-05-26 21:01:19 -07:00
commit afffb15196
4 changed files with 37 additions and 34 deletions

3
.gitignore vendored
View File

@ -20,3 +20,6 @@ _cgo_export.*
_testmain.go _testmain.go
*.exe *.exe
.idea/
*.iml

View File

@ -30,8 +30,8 @@ var upgrader = websocket.Upgrader{
WriteBufferSize: 1024, WriteBufferSize: 1024,
} }
// connection is an middleman between the websocket connection and the hub. // Conn is an middleman between the websocket connection and the hub.
type connection struct { type Conn struct {
// The websocket connection. // The websocket connection.
ws *websocket.Conn ws *websocket.Conn
@ -40,9 +40,9 @@ type connection struct {
} }
// readPump pumps messages from the websocket connection to the hub. // readPump pumps messages from the websocket connection to the hub.
func (c *connection) readPump() { func (c *Conn) readPump() {
defer func() { defer func() {
h.unregister <- c hub.unregister <- c
c.ws.Close() c.ws.Close()
}() }()
c.ws.SetReadLimit(maxMessageSize) c.ws.SetReadLimit(maxMessageSize)
@ -56,18 +56,18 @@ func (c *connection) readPump() {
} }
break break
} }
h.broadcast <- message hub.broadcast <- message
} }
} }
// write writes a message with the given message type and payload. // write writes a message with the given message type and payload.
func (c *connection) write(mt int, payload []byte) error { func (c *Conn) write(mt int, payload []byte) error {
c.ws.SetWriteDeadline(time.Now().Add(writeWait)) c.ws.SetWriteDeadline(time.Now().Add(writeWait))
return c.ws.WriteMessage(mt, payload) return c.ws.WriteMessage(mt, payload)
} }
// writePump pumps messages from the hub to the websocket connection. // writePump pumps messages from the hub to the websocket connection.
func (c *connection) writePump() { func (c *Conn) writePump() {
ticker := time.NewTicker(pingPeriod) ticker := time.NewTicker(pingPeriod)
defer func() { defer func() {
ticker.Stop() ticker.Stop()
@ -98,8 +98,8 @@ func serveWs(w http.ResponseWriter, r *http.Request) {
log.Println(err) log.Println(err)
return return
} }
c := &connection{send: make(chan []byte, 256), ws: ws} conn := &Conn{send: make(chan []byte, 256), ws: ws}
h.register <- c hub.register <- conn
go c.writePump() go conn.writePump()
c.readPump() conn.readPump()
} }

View File

@ -6,44 +6,44 @@ package main
// hub maintains the set of active connections and broadcasts messages to the // hub maintains the set of active connections and broadcasts messages to the
// connections. // connections.
type hub struct { type Hub struct {
// Registered connections. // Registered connections.
connections map[*connection]bool connections map[*Conn]bool
// Inbound messages from the connections. // Inbound messages from the connections.
broadcast chan []byte broadcast chan []byte
// Register requests from the connections. // Register requests from the connections.
register chan *connection register chan *Conn
// Unregister requests from connections. // Unregister requests from connections.
unregister chan *connection unregister chan *Conn
} }
var h = hub{ var hub = Hub{
broadcast: make(chan []byte), broadcast: make(chan []byte),
register: make(chan *connection), register: make(chan *Conn),
unregister: make(chan *connection), unregister: make(chan *Conn),
connections: make(map[*connection]bool), connections: make(map[*Conn]bool),
} }
func (h *hub) run() { func (h *Hub) run() {
for { for {
select { select {
case c := <-h.register: case conn := <-h.register:
h.connections[c] = true h.connections[conn] = true
case c := <-h.unregister: case conn := <-h.unregister:
if _, ok := h.connections[c]; ok { if _, ok := h.connections[conn]; ok {
delete(h.connections, c) delete(h.connections, conn)
close(c.send) close(conn.send)
} }
case m := <-h.broadcast: case message := <-h.broadcast:
for c := range h.connections { for conn := range h.connections {
select { select {
case c.send <- m: case conn.send <- message:
default: default:
close(c.send) close(conn.send)
delete(h.connections, c) delete(hub.connections, conn)
} }
} }
} }

View File

@ -12,7 +12,7 @@ import (
) )
var addr = flag.String("addr", ":8080", "http service address") 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) { func serveHome(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" { if r.URL.Path != "/" {
@ -24,12 +24,12 @@ func serveHome(w http.ResponseWriter, r *http.Request) {
return return
} }
w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Header().Set("Content-Type", "text/html; charset=utf-8")
homeTempl.Execute(w, r.Host) homeTemplate.Execute(w, r.Host)
} }
func main() { func main() {
flag.Parse() flag.Parse()
go h.run() go hub.run()
http.HandleFunc("/", serveHome) http.HandleFunc("/", serveHome)
http.HandleFunc("/ws", serveWs) http.HandleFunc("/ws", serveWs)
err := http.ListenAndServe(*addr, nil) err := http.ListenAndServe(*addr, nil)