startWriteHandler starts the write loop. The method has the following tasks: * ping the client in the interval provided as parameter * write messages send by the channel to the client * on errors exit the loop.
(pingPeriod time.Duration)
| 79 | // * write messages send by the channel to the client |
| 80 | // * on errors exit the loop. |
| 81 | func (c *client) startWriteHandler(pingPeriod time.Duration) { |
| 82 | pingTicker := time.NewTicker(pingPeriod) |
| 83 | defer func() { |
| 84 | c.NotifyClose() |
| 85 | pingTicker.Stop() |
| 86 | }() |
| 87 | |
| 88 | for { |
| 89 | select { |
| 90 | case message, ok := <-c.write: |
| 91 | if !ok { |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | c.conn.SetWriteDeadline(time.Now().Add(writeWait)) |
| 96 | if err := writeJSON(c.conn, message); err != nil { |
| 97 | printWebSocketError("WriteError", err) |
| 98 | return |
| 99 | } |
| 100 | case <-pingTicker.C: |
| 101 | c.conn.SetWriteDeadline(time.Now().Add(writeWait)) |
| 102 | if err := ping(c.conn); err != nil { |
| 103 | printWebSocketError("PingError", err) |
| 104 | return |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func printWebSocketError(prefix string, err error) { |
| 111 | closeError, ok := err.(*websocket.CloseError) |
no test coverage detected