-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzsocket.go
More file actions
136 lines (109 loc) · 3.96 KB
/
zsocket.go
File metadata and controls
136 lines (109 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package zsocket
import (
"compress/flate"
"errors"
"time"
)
// ==============================
// Public message & close types
// ==============================
// MessageType represents the WebSocket message type.
type MessageType byte
const (
TextMessage MessageType = 1
BinaryMessage MessageType = 2
CloseMessage MessageType = 8
PingMessage MessageType = 9
PongMessage MessageType = 10
)
// CloseCode follows RFC 6455 close codes (select common ones here).
type CloseCode uint16
const (
CloseNormalClosure CloseCode = 1000
CloseGoingAway CloseCode = 1001
CloseProtocolError CloseCode = 1002
CloseUnsupportedData CloseCode = 1003
CloseNoStatusRcvd CloseCode = 1005 // reserved - do not send
CloseNoStatusReceived CloseCode = CloseNoStatusRcvd
CloseAbnormalClosure CloseCode = 1006 // reserved - do not send
CloseInvalidFramePayloadData CloseCode = 1007
ClosePolicyViolation CloseCode = 1008
CloseMessageTooBig CloseCode = 1009
CloseMandatoryExtension CloseCode = 1010
CloseInternalServerErr CloseCode = 1011
CloseTLSHandshake CloseCode = 1015
)
// ==============================
// Config
// ==============================
// Config controls WebSocket server behavior during handshake and runtime.
type Config struct {
// AllowedOrigins is used to check request Origin. Empty means "allow all".
AllowedOrigins []string
// Subprotocols contains allowed subprotocol names.
Subprotocols []string
// HandshakeTimeout controls the HTTP upgrade deadline.
HandshakeTimeout time.Duration
// ReadLimit caps the maximum message payload (bytes). 0 means no limit.
ReadLimit int64
// ReadBufferSize / WriteBufferSize are used for buffered I/O.
ReadBufferSize int
WriteBufferSize int
// PingInterval sets how often to send pings. 0 disables keepalive pings.
PingInterval time.Duration
// PongWait is how long to wait for a pong after a ping. Must be > 0 if PingInterval > 0.
PongWait time.Duration
// WriteTimeout caps write operations (frames & control frames).
WriteTimeout time.Duration
// CheckOrigin allows custom origin validation. If set, overrides AllowedOrigins.
CheckOrigin func(origin string) bool
// Logger is optional. If nil, logging is no-op.
Logger Logger
// EnableCompression enables per-message deflate negotiation.
EnableCompression bool
// CompressionLevel controls flate level when compression is enabled.
CompressionLevel int
// WriteBufferPool optionally provides reusable write buffers.
WriteBufferPool BufferPool
}
// DefaultConfig returns a practical default config for servers.
func DefaultConfig() Config {
return Config{
AllowedOrigins: nil,
Subprotocols: nil,
HandshakeTimeout: 5 * time.Second,
ReadLimit: 16 << 20, // 16 MiB
ReadBufferSize: 4096,
WriteBufferSize: 4096,
PingInterval: 30 * time.Second,
PongWait: 15 * time.Second,
WriteTimeout: 10 * time.Second,
Logger: NopLogger{},
CompressionLevel: flate.DefaultCompression,
}
}
// ==============================
// Logging interface
// ==============================
type Logger interface {
Debugf(format string, args ...any)
Infof(format string, args ...any)
Warnf(format string, args ...any)
Errorf(format string, args ...any)
}
type NopLogger struct{}
func (NopLogger) Debugf(string, ...any) {}
func (NopLogger) Infof(string, ...any) {}
func (NopLogger) Warnf(string, ...any) {}
func (NopLogger) Errorf(string, ...any) {}
// ==============================
// Public errors
// ==============================
var (
ErrBadRequest = errors.New("zsocket: bad websocket upgrade request")
ErrHandshakeFailed = errors.New("zsocket: handshake failed")
ErrOriginNotAllowed = errors.New("zsocket: origin not allowed")
ErrSubprotocolRejected = errors.New("zsocket: subprotocol rejected")
ErrClosed = errors.New("zsocket: connection closed")
ErrTimeout = errors.New("zsocket: timeout")
)