readPump pumps messages from the websocket connection to the hub. The application runs readPump in a per-connection goroutine. The application ensures that there is at most one reader on a connection by executing all reads from this goroutine.
()
| 54 | // ensures that there is at most one reader on a connection by executing all |
| 55 | // reads from this goroutine. |
| 56 | func (c *Client) readPump() { |
| 57 | defer func() { |
| 58 | c.hub.unregister <- c |
| 59 | c.conn.Close() |
| 60 | }() |
| 61 | c.conn.SetReadLimit(maxMessageSize) |
| 62 | c.conn.SetReadDeadline(time.Now().Add(pongWait)) |
| 63 | c.conn.SetPongHandler(func(string) error { c.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil }) |
| 64 | for { |
| 65 | _, message, err := c.conn.ReadMessage() |
| 66 | if err != nil { |
| 67 | if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { |
| 68 | log.Printf("error: %v", err) |
| 69 | } |
| 70 | break |
| 71 | } |
| 72 | message = bytes.TrimSpace(bytes.Replace(message, newline, space, -1)) |
| 73 | c.hub.broadcast <- message |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // writePump pumps messages from the hub to the websocket connection. |
| 78 | // |
no test coverage detected