good morning!!!!

Skip to content
Snippets Groups Projects
Unverified Commit e9dfcc4e authored by Anmol Sethi's avatar Anmol Sethi
Browse files

Improve Close UX

Closes #78
parent 4a611672
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"os"
"runtime" "runtime"
"sync" "sync"
"sync/atomic" "sync/atomic"
...@@ -353,6 +354,12 @@ func (c *Conn) writePong(p []byte) error { ...@@ -353,6 +354,12 @@ func (c *Conn) writePong(p []byte) error {
// Close closes the WebSocket connection with the given status code and reason. // 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. // 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 { func (c *Conn) Close(code StatusCode, reason string) error {
err := c.exportedClose(code, reason) err := c.exportedClose(code, reason)
if err != nil { if err != nil {
...@@ -372,17 +379,14 @@ func (c *Conn) exportedClose(code StatusCode, reason string) error { ...@@ -372,17 +379,14 @@ func (c *Conn) exportedClose(code StatusCode, reason string) error {
// Definitely worth seeing what popular browsers do later. // Definitely worth seeing what popular browsers do later.
p, err := ce.bytes() p, err := ce.bytes()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "failed to marshal close frame: %v\n", err)
ce = CloseError{ ce = CloseError{
Code: StatusInternalError, Code: StatusInternalError,
} }
p, _ = ce.bytes() p, _ = ce.bytes()
} }
cerr := c.writeClose(p, ce) return c.writeClose(p, ce)
if err != nil {
return err
}
return cerr
} }
func (c *Conn) writeClose(p []byte, cerr CloseError) error { func (c *Conn) writeClose(p []byte, cerr CloseError) error {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment