Close performs the WebSocket close handshake with the given status code and reason. It will write a WebSocket close frame with a timeout of 5s and then wait 5s for the peer to send a close frame. All data messages received from the peer during the close handshake will be discarded. The connection
(code StatusCode, reason string)
| 97 | // Close will unblock all goroutines interacting with the connection once |
| 98 | // complete. |
| 99 | func (c *Conn) Close(code StatusCode, reason string) (err error) { |
| 100 | defer errd.Wrap(&err, "failed to close WebSocket") |
| 101 | |
| 102 | if c.casClosing() { |
| 103 | err = c.waitGoroutines() |
| 104 | if err != nil { |
| 105 | return err |
| 106 | } |
| 107 | return net.ErrClosed |
| 108 | } |
| 109 | defer func() { |
| 110 | if errors.Is(err, net.ErrClosed) { |
| 111 | err = nil |
| 112 | } |
| 113 | }() |
| 114 | |
| 115 | err = c.closeHandshake(code, reason) |
| 116 | |
| 117 | err2 := c.close() |
| 118 | if err == nil && err2 != nil { |
| 119 | err = err2 |
| 120 | } |
| 121 | |
| 122 | err2 = c.waitGoroutines() |
| 123 | if err == nil && err2 != nil { |
| 124 | err = err2 |
| 125 | } |
| 126 | |
| 127 | return err |
| 128 | } |
| 129 | |
| 130 | // CloseNow closes the WebSocket connection without attempting a close handshake. |
| 131 | // Use when you do not want the overhead of the close handshake. |