Skip to content

Commit f1a37d6

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

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

websocket.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,12 @@ func (c *Conn) writePong(p []byte) error {
353353

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

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

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

0 commit comments

Comments
 (0)