-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
151 lines (130 loc) · 4.12 KB
/
options.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package wso
import (
"time"
"github.com/sirupsen/logrus"
)
type MessageProcessor interface {
OnTextMessage(websocket *Websocket, message []byte) error
OnBinaryMessage(websocket *Websocket, message []byte) error
}
type WebsocketCallOption struct {
applyFunc func(opt *websocketOptions)
}
type websocketOptions struct {
logger *logrus.Logger
connectionPool *ConnectionPool
autoReplyPong bool
// 配置是否跳过 Ping 消息,默认跳过
onMessageSkipPingMessage bool
// 配置是否跳过 Pong 消息,默认跳过
onMessageSkipPongMessage bool
// 心跳 Ping 消息,默认值为 "❤️"
pingMessageHeart string
// 心跳 Pong 消息,默认值为 "💚"
pongMessageHeart string
// 配置最大消息大小,默认值为 4MB
readLimit int64
// 配置读取消息超时时间,默认值为 1 分钟
readDeadline time.Duration
// 配置写消息超时时间,默认值为 1 分钟
writeDeadline time.Duration
// 配置 Ping 消息发送间隔,默认值为 20 秒
pingMessagePeriod time.Duration
processor MessageProcessor
}
// WithLogger 设置日志记录器
func WithLogger(logger *logrus.Logger) WebsocketCallOption {
return WebsocketCallOption{
applyFunc: func(opt *websocketOptions) {
opt.logger = logger
},
}
}
// WithConnectionPool 设置连接池
func WithConnectionPool(pool *ConnectionPool) WebsocketCallOption {
return WebsocketCallOption{
applyFunc: func(opt *websocketOptions) {
opt.connectionPool = pool
},
}
}
// WithAutoReplyPong 设置是否自动回复 Pong 消息,默认值为 true
func WithAutoReplyPong(autoReplyPong bool) WebsocketCallOption {
return WebsocketCallOption{
applyFunc: func(opt *websocketOptions) {
opt.autoReplyPong = autoReplyPong
},
}
}
// WithOnMessageSkipStrategy 设置是否跳过 Ping 消息和 Pong 消息,默认值为 true
func WithOnMessageSkipStrategy(skipPingMessage, skipPongMessage bool) WebsocketCallOption {
return WebsocketCallOption{
applyFunc: func(opt *websocketOptions) {
opt.onMessageSkipPingMessage = skipPingMessage
opt.onMessageSkipPongMessage = skipPongMessage
},
}
}
// WithReadLimit 设置读取消息的最大长度,超过该长度将会断开连接,单位:字节
func WithReadLimit(limit int64) WebsocketCallOption {
return WebsocketCallOption{
applyFunc: func(opt *websocketOptions) {
opt.readLimit = limit
},
}
}
// WithReadDeadline 设置读取消息的超时时间
func WithReadDeadline(deadline time.Duration) WebsocketCallOption {
return WebsocketCallOption{
applyFunc: func(opt *websocketOptions) {
opt.readDeadline = deadline
},
}
}
// WithWriteDeadline 设置写消息的超时时间
func WithWriteDeadline(deadline time.Duration) WebsocketCallOption {
return WebsocketCallOption{
applyFunc: func(opt *websocketOptions) {
opt.writeDeadline = deadline
},
}
}
// WithPingMessagePeriod 设置 Ping 消息的发送间隔
func WithPingMessagePeriod(period time.Duration) WebsocketCallOption {
return WebsocketCallOption{
applyFunc: func(opt *websocketOptions) {
opt.pingMessagePeriod = period
},
}
}
// 注册消息监听器,当收到消息时将会调用所有注册的消息监听器。消息监听器将可以返回一个 error 类型的错误,如果返回的错误不为 nil,将会关闭连接
func WithMessageProcessor(processor MessageProcessor) WebsocketCallOption {
return WebsocketCallOption{
applyFunc: func(opt *websocketOptions) {
opt.processor = processor
},
}
}
// applyOptions 应用选项
func applyOptions(callOptions ...WebsocketCallOption) *websocketOptions {
defaultOptions := &websocketOptions{
logger: logrus.New(),
onMessageSkipPingMessage: true,
onMessageSkipPongMessage: true,
pingMessageHeart: "❤️",
pongMessageHeart: "💚",
readLimit: int64(4 * BytesUnitMB),
readDeadline: time.Minute * 1,
writeDeadline: time.Minute * 1,
pingMessagePeriod: time.Second * 20,
}
if len(callOptions) == 0 {
return defaultOptions
}
optCopy := &websocketOptions{}
*optCopy = *defaultOptions
for _, f := range callOptions {
f.applyFunc(optCopy)
}
return optCopy
}