[822] | 1 | package websocket
|
---|
| 2 |
|
---|
| 3 | import (
|
---|
| 4 | "errors"
|
---|
| 5 | "fmt"
|
---|
| 6 | )
|
---|
| 7 |
|
---|
| 8 | // StatusCode represents a WebSocket status code.
|
---|
| 9 | // https://tools.ietf.org/html/rfc6455#section-7.4
|
---|
| 10 | type StatusCode int
|
---|
| 11 |
|
---|
| 12 | // https://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
|
---|
| 13 | //
|
---|
| 14 | // These are only the status codes defined by the protocol.
|
---|
| 15 | //
|
---|
| 16 | // You can define custom codes in the 3000-4999 range.
|
---|
| 17 | // The 3000-3999 range is reserved for use by libraries, frameworks and applications.
|
---|
| 18 | // The 4000-4999 range is reserved for private use.
|
---|
| 19 | const (
|
---|
| 20 | StatusNormalClosure StatusCode = 1000
|
---|
| 21 | StatusGoingAway StatusCode = 1001
|
---|
| 22 | StatusProtocolError StatusCode = 1002
|
---|
| 23 | StatusUnsupportedData StatusCode = 1003
|
---|
| 24 |
|
---|
| 25 | // 1004 is reserved and so unexported.
|
---|
| 26 | statusReserved StatusCode = 1004
|
---|
| 27 |
|
---|
| 28 | // StatusNoStatusRcvd cannot be sent in a close message.
|
---|
| 29 | // It is reserved for when a close message is received without
|
---|
| 30 | // a status code.
|
---|
| 31 | StatusNoStatusRcvd StatusCode = 1005
|
---|
| 32 |
|
---|
| 33 | // StatusAbnormalClosure is exported for use only with Wasm.
|
---|
| 34 | // In non Wasm Go, the returned error will indicate whether the
|
---|
| 35 | // connection was closed abnormally.
|
---|
| 36 | StatusAbnormalClosure StatusCode = 1006
|
---|
| 37 |
|
---|
| 38 | StatusInvalidFramePayloadData StatusCode = 1007
|
---|
| 39 | StatusPolicyViolation StatusCode = 1008
|
---|
| 40 | StatusMessageTooBig StatusCode = 1009
|
---|
| 41 | StatusMandatoryExtension StatusCode = 1010
|
---|
| 42 | StatusInternalError StatusCode = 1011
|
---|
| 43 | StatusServiceRestart StatusCode = 1012
|
---|
| 44 | StatusTryAgainLater StatusCode = 1013
|
---|
| 45 | StatusBadGateway StatusCode = 1014
|
---|
| 46 |
|
---|
| 47 | // StatusTLSHandshake is only exported for use with Wasm.
|
---|
| 48 | // In non Wasm Go, the returned error will indicate whether there was
|
---|
| 49 | // a TLS handshake failure.
|
---|
| 50 | StatusTLSHandshake StatusCode = 1015
|
---|
| 51 | )
|
---|
| 52 |
|
---|
| 53 | // CloseError is returned when the connection is closed with a status and reason.
|
---|
| 54 | //
|
---|
| 55 | // Use Go 1.13's errors.As to check for this error.
|
---|
| 56 | // Also see the CloseStatus helper.
|
---|
| 57 | type CloseError struct {
|
---|
| 58 | Code StatusCode
|
---|
| 59 | Reason string
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | func (ce CloseError) Error() string {
|
---|
| 63 | return fmt.Sprintf("status = %v and reason = %q", ce.Code, ce.Reason)
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | // CloseStatus is a convenience wrapper around Go 1.13's errors.As to grab
|
---|
| 67 | // the status code from a CloseError.
|
---|
| 68 | //
|
---|
| 69 | // -1 will be returned if the passed error is nil or not a CloseError.
|
---|
| 70 | func CloseStatus(err error) StatusCode {
|
---|
| 71 | var ce CloseError
|
---|
| 72 | if errors.As(err, &ce) {
|
---|
| 73 | return ce.Code
|
---|
| 74 | }
|
---|
| 75 | return -1
|
---|
| 76 | }
|
---|