Skip to content

Commit e9dfcc4

Browse files
committed
Improve Close UX
Closes #78
1 parent 4a61167 commit e9dfcc4

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

websocket.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"fmt"
77
"io"
8+
"os"
89
"runtime"
910
"sync"
1011
"sync/atomic"
@@ -353,6 +354,12 @@ func (c *Conn) writePong(p []byte) error {
353354

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

381-
cerr := c.writeClose(p, ce)
382-
if err != nil {
383-
return err
384-
}
385-
return cerr
389+
return c.writeClose(p, ce)
386390
}
387391

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

0 commit comments

Comments
 (0)