source: code/trunk/vendor/nhooyr.io/websocket/close.go@ 822

Last change on this file since 822 was 822, checked in by yakumo.izuru, 22 months ago

Prefer immortal.run over runit and rc.d, use vendored modules
for convenience.

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

File size: 2.3 KB
Line 
1package websocket
2
3import (
4 "errors"
5 "fmt"
6)
7
8// StatusCode represents a WebSocket status code.
9// https://tools.ietf.org/html/rfc6455#section-7.4
10type 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.
19const (
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.
57type CloseError struct {
58 Code StatusCode
59 Reason string
60}
61
62func (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.
70func CloseStatus(err error) StatusCode {
71 var ce CloseError
72 if errors.As(err, &ce) {
73 return ce.Code
74 }
75 return -1
76}
Note: See TracBrowser for help on using the repository browser.