@@ -15,11 +15,13 @@ import (
1515// https://tools.ietf.org/html/rfc6455#section-7.4
1616type StatusCode int
1717
18- // These codes were retrieved from:
1918// https://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
2019//
21- // The defined constants only represent the status codes registered with IANA.
22- // The 4000-4999 range of status codes is reserved for arbitrary use by applications.
20+ // These are only the status codes defined by the protocol.
21+ //
22+ // You can define custom codes in the 3000-4999 range.
23+ // The 3000-3999 range is reserved for use by libraries, frameworks and applications.
24+ // The 4000-4999 range is reserved for private use.
2325const (
2426 StatusNormalClosure StatusCode = 1000
2527 StatusGoingAway StatusCode = 1001
@@ -31,11 +33,12 @@ const (
3133
3234 // StatusNoStatusRcvd cannot be sent in a close message.
3335 // It is reserved for when a close message is received without
34- // an explicit status.
36+ // a status code .
3537 StatusNoStatusRcvd StatusCode = 1005
3638
37- // StatusAbnormalClosure is only exported for use with Wasm.
38- // In non Wasm Go, the returned error will indicate whether the connection was closed or not or what happened.
39+ // StatusAbnormalClosure is exported for use only with Wasm.
40+ // In non Wasm Go, the returned error will indicate whether the
41+ // connection was closed abnormally.
3942 StatusAbnormalClosure StatusCode = 1006
4043
4144 StatusInvalidFramePayloadData StatusCode = 1007
@@ -48,15 +51,15 @@ const (
4851 StatusBadGateway StatusCode = 1014
4952
5053 // StatusTLSHandshake is only exported for use with Wasm.
51- // In non Wasm Go, the returned error will indicate whether there was a TLS handshake failure.
54+ // In non Wasm Go, the returned error will indicate whether there was
55+ // a TLS handshake failure.
5256 StatusTLSHandshake StatusCode = 1015
5357)
5458
55- // CloseError represents a WebSocket close frame.
56- // It is returned by Conn's methods when a WebSocket close frame is received from
57- // the peer.
58- // You will need to use the https://golang.org/pkg/errors/#As function, new in Go 1.13,
59- // to check for this error. See the CloseError example.
59+ // CloseError is returned when the connection is closed with a status and reason.
60+ //
61+ // Use Go 1.13's errors.As to check for this error.
62+ // Also see the CloseStatus helper.
6063type CloseError struct {
6164 Code StatusCode
6265 Reason string
@@ -66,9 +69,10 @@ func (ce CloseError) Error() string {
6669 return fmt .Sprintf ("status = %v and reason = %q" , ce .Code , ce .Reason )
6770}
6871
69- // CloseStatus is a convenience wrapper around errors.As to grab
70- // the status code from a *CloseError. If the passed error is nil
71- // or not a *CloseError, the returned StatusCode will be -1.
72+ // CloseStatus is a convenience wrapper around Go 1.13's errors.As to grab
73+ // the status code from a CloseError.
74+ //
75+ // -1 will be returned if the passed error is nil or not a CloseError.
7276func CloseStatus (err error ) StatusCode {
7377 var ce CloseError
7478 if errors .As (err , & ce ) {
@@ -77,19 +81,16 @@ func CloseStatus(err error) StatusCode {
7781 return - 1
7882}
7983
80- // Close closes the WebSocket connection with the given status code and reason.
84+ // Close performs the WebSocket close handshake with the given status code and reason.
8185//
8286// It will write a WebSocket close frame with a timeout of 5s and then wait 5s for
8387// the peer to send a close frame.
84- // Thus, it implements the full WebSocket close handshake.
85- // All data messages received from the peer during the close handshake
86- // will be discarded.
88+ // All data messages received from the peer during the close handshake will be discarded.
8789//
8890// The connection can only be closed once. Additional calls to Close
8991// are no-ops.
9092//
91- // The maximum length of reason must be 125 bytes otherwise an internal
92- // error will be sent to the peer. For this reason, you should avoid
93+ // The maximum length of reason must be 125 bytes. Avoid
9394// sending a dynamic reason.
9495//
9596// Close will unblock all goroutines interacting with the connection once
0 commit comments