-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
74 lines (66 loc) · 1.87 KB
/
option.go
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
package gows
import "time"
// Option is used to alter the default behavior of the
// websocket connection. Options can be optionally passed
// to the Upgrade function.
type Option func(ws *websocket)
type options struct {
maxMessageSize int64
writeTimeout time.Duration
pongTimeout time.Duration
pingPeriod time.Duration // must be less than pongTimeout
disablePingPong bool
}
var defaultOptions = options{
maxMessageSize: 2048,
writeTimeout: 10 * time.Second,
pongTimeout: 60 * time.Second,
pingPeriod: (60 * time.Second * 9) / 10,
disablePingPong: false,
}
// MaxMessageSize defines the maximum size allowed for messages
// that are read from clients. If a client attempts to send a larger
// message, an error will occur.
//
// Default: 2048
func MaxMessageSize(size int64) Option {
if size < 0 {
panic("size must be non-negative")
}
return func(ws *websocket) {
ws.options.maxMessageSize = size
}
}
// WriteTimeout defines how long to wait for messages to be sent
// to the client before aborting. Setting it to zero means no timeout.
//
// Default: 10 seconds
func WriteTimeout(t time.Duration) Option {
return func(ws *websocket) {
ws.options.writeTimeout = t
}
}
// PongTimeout defines how long to wait for a pong response before
// determining the connection to be dead. Ping messages will be sent
// in intervals from which pong responses are expected.
//
// Default: 60 seconds
func PongTimeout(t time.Duration) Option {
if t == 0 {
panic("timeout can not be zero")
}
return func(ws *websocket) {
ws.options.pongTimeout = t
ws.options.pingPeriod = (t * 9) / 10
}
}
// DisablePingPong disables the pong/pong mecahnism and means that
// even if there is no communication the connection will be considered
// alive.
//
// Default: false
func DisablePingPong() Option {
return func(ws *websocket) {
ws.options.disablePingPong = true
}
}