Skip to content

Commit

Permalink
Improve Close UX
Browse files Browse the repository at this point in the history
Closes #78
  • Loading branch information
nhooyr committed May 17, 2019
1 parent 4a61167 commit f1a37d6
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,12 @@ func (c *Conn) writePong(p []byte) error {

// Close closes the WebSocket connection with the given status code and reason.
// It will write a WebSocket close frame with a timeout of 5 seconds.
// The connection can only be closed once. Additional calls to Close
// are no-ops.
// The maximum length of reason must be 125 bytes otherwise an internal
// error will be sent to the peer. For this reason, you should avoid
// sending a dynamic reason.
// Close will unblock all goroutines interacting with the connection.
func (c *Conn) Close(code StatusCode, reason string) error {
err := c.exportedClose(code, reason)
if err != nil {
Expand All @@ -372,17 +378,14 @@ func (c *Conn) exportedClose(code StatusCode, reason string) error {
// Definitely worth seeing what popular browsers do later.
p, err := ce.bytes()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to marshal close frame: %v\n", err)
ce = CloseError{
Code: StatusInternalError,
}
p, _ = ce.bytes()
}

cerr := c.writeClose(p, ce)
if err != nil {
return err
}
return cerr
return c.writeClose(p, ce)
}

func (c *Conn) writeClose(p []byte, cerr CloseError) error {
Expand Down

0 comments on commit f1a37d6

Please sign in to comment.