-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions_websocket.go
More file actions
93 lines (80 loc) · 2.73 KB
/
Copy pathoptions_websocket.go
File metadata and controls
93 lines (80 loc) · 2.73 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
package servex
import "time"
// WebSocketConfig holds configuration for WebSocket connections.
// WebSocket support is activated lazily when server.WS() or server.WSHub() is called.
// These options just set parameters — there is no Enabled flag.
//
// Example:
//
// server, _ := servex.NewServer(
// servex.WithWebSocketMaxMessageSize(64 << 10),
// servex.WithWebSocketPingInterval(20 * time.Second),
// )
// server.WS("/ws/echo", echoHandler) // WebSocket is now active
type WebSocketConfig struct {
// MaxMessageSize is the maximum size of a single WebSocket message in bytes.
// Messages exceeding this limit cause the connection to close with StatusMessageTooBig.
//
// Default: 32768 (32 KB)
MaxMessageSize int64
// PingInterval is how often the server sends ping frames to clients.
// Set to negative value to disable server-initiated pings.
//
// Default: 30s
PingInterval time.Duration
// PongTimeout is how long to wait for a pong response before closing the connection.
// Must be less than PingInterval when both are set.
//
// Default: 10s
PongTimeout time.Duration
// AllowedOrigins controls which origins may connect via WebSocket.
// Checked during the HTTP upgrade handshake. If empty, all origins are allowed.
AllowedOrigins []string
// EnableCompression enables per-message deflate compression (RFC 7692).
//
// Default: false
EnableCompression bool
}
// WebSocket defaults.
const (
defaultWSMaxMessageSize = 32 << 10 // 32 KB
defaultWSPingInterval = 30 * time.Second
defaultWSPongTimeout = 10 * time.Second
)
// WithWebSocketConfig sets the full WebSocket configuration.
func WithWebSocketConfig(cfg WebSocketConfig) Option {
return func(o *Options) {
o.WebSocket = cfg
}
}
// WithWebSocketMaxMessageSize sets the maximum size of a single WebSocket message.
func WithWebSocketMaxMessageSize(size int64) Option {
return func(o *Options) {
o.WebSocket.MaxMessageSize = size
}
}
// WithWebSocketPingInterval sets how often the server sends ping frames.
// Set to negative value to disable server-initiated pings.
func WithWebSocketPingInterval(d time.Duration) Option {
return func(o *Options) {
o.WebSocket.PingInterval = d
}
}
// WithWebSocketPongTimeout sets how long to wait for a pong response.
func WithWebSocketPongTimeout(d time.Duration) Option {
return func(o *Options) {
o.WebSocket.PongTimeout = d
}
}
// WithWebSocketAllowedOrigins sets which origins may connect via WebSocket.
func WithWebSocketAllowedOrigins(origins ...string) Option {
return func(o *Options) {
o.WebSocket.AllowedOrigins = origins
}
}
// WithWebSocketCompression enables per-message deflate compression (RFC 7692).
func WithWebSocketCompression() Option {
return func(o *Options) {
o.WebSocket.EnableCompression = true
}
}