mirror of https://github.com/gorilla/websocket.git
Meaningful names for connection, message
This commit is contained in:
parent
a6108176e8
commit
be95f72b73
|
@ -40,16 +40,16 @@ 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 (conn *connection) readPump() {
|
||||||
defer func() {
|
defer func() {
|
||||||
mainHub.unregister <- c
|
mainHub.unregister <- conn
|
||||||
c.ws.Close()
|
conn.ws.Close()
|
||||||
}()
|
}()
|
||||||
c.ws.SetReadLimit(maxMessageSize)
|
conn.ws.SetReadLimit(maxMessageSize)
|
||||||
c.ws.SetReadDeadline(time.Now().Add(pongWait))
|
conn.ws.SetReadDeadline(time.Now().Add(pongWait))
|
||||||
c.ws.SetPongHandler(func(string) error { c.ws.SetReadDeadline(time.Now().Add(pongWait)); return nil })
|
conn.ws.SetPongHandler(func(string) error { conn.ws.SetReadDeadline(time.Now().Add(pongWait)); return nil })
|
||||||
for {
|
for {
|
||||||
_, message, err := c.ws.ReadMessage()
|
_, message, err := conn.ws.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
|
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
|
||||||
log.Printf("error: %v", err)
|
log.Printf("error: %v", err)
|
||||||
|
@ -61,30 +61,30 @@ func (c *connection) readPump() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 (conn *connection) write(mt int, payload []byte) error {
|
||||||
c.ws.SetWriteDeadline(time.Now().Add(writeWait))
|
conn.ws.SetWriteDeadline(time.Now().Add(writeWait))
|
||||||
return c.ws.WriteMessage(mt, payload)
|
return conn.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 (conn *connection) writePump() {
|
||||||
ticker := time.NewTicker(pingPeriod)
|
ticker := time.NewTicker(pingPeriod)
|
||||||
defer func() {
|
defer func() {
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
c.ws.Close()
|
conn.ws.Close()
|
||||||
}()
|
}()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case message, ok := <-c.send:
|
case message, ok := <-conn.send:
|
||||||
if !ok {
|
if !ok {
|
||||||
c.write(websocket.CloseMessage, []byte{})
|
conn.write(websocket.CloseMessage, []byte{})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := c.write(websocket.TextMessage, message); err != nil {
|
if err := conn.write(websocket.TextMessage, message); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
if err := c.write(websocket.PingMessage, []byte{}); err != nil {
|
if err := conn.write(websocket.PingMessage, []byte{}); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 := &connection{send: make(chan []byte, 256), ws: ws}
|
||||||
mainHub.register <- c
|
mainHub.register <- conn
|
||||||
go c.writePump()
|
go conn.writePump()
|
||||||
c.readPump()
|
conn.readPump()
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,20 +30,20 @@ var mainHub = hub{
|
||||||
func (hub *hub) run() {
|
func (hub *hub) run() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case c := <-hub.register:
|
case conn := <-hub.register:
|
||||||
hub.connections[c] = true
|
hub.connections[conn] = true
|
||||||
case c := <-hub.unregister:
|
case conn := <-hub.unregister:
|
||||||
if _, ok := hub.connections[c]; ok {
|
if _, ok := hub.connections[conn]; ok {
|
||||||
delete(hub.connections, c)
|
delete(hub.connections, conn)
|
||||||
close(c.send)
|
close(conn.send)
|
||||||
}
|
}
|
||||||
case m := <-hub.broadcast:
|
case message := <-hub.broadcast:
|
||||||
for c := range hub.connections {
|
for conn := range hub.connections {
|
||||||
select {
|
select {
|
||||||
case c.send <- m:
|
case conn.send <- message:
|
||||||
default:
|
default:
|
||||||
close(c.send)
|
close(conn.send)
|
||||||
delete(hub.connections, c)
|
delete(hub.connections, conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue