Meaningful names for connection, message

This commit is contained in:
Jean de Klerk 2016-05-18 08:57:16 -06:00
parent a6108176e8
commit be95f72b73
2 changed files with 31 additions and 31 deletions

View File

@ -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()
}

View File

@ -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)
}
}
}