-
Notifications
You must be signed in to change notification settings - Fork 1
/
parameter.go
75 lines (63 loc) · 1.73 KB
/
parameter.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
package wsconn
import (
"github.com/gorilla/websocket"
"time"
)
type WsParameter struct {
WsUrl string
ReqHeaders map[string][]string
Dialer *websocket.Dialer
MessageHandleFunc func([]byte) error
ReSubscribeFunc func(ws *WsConn) error // 短线重连后触发,你可以重新订阅,因为有些订阅跟当前时间相关
ErrorHandleFunc func(err error)
AutoReconnect bool // 自动重连(默认: true)
ReSubscribe bool // 自动重新订阅(默认: true)
IsDump bool
readDeadLineTime time.Duration
}
type WsParameterOption func(p *WsParameter)
func WsUrlOption(wsUrl string) WsParameterOption {
return func(p *WsParameter) {
p.WsUrl = wsUrl
}
}
func WsReqHeaderOption(key, value string) WsParameterOption {
return func(p *WsParameter) {
p.ReqHeaders[key] = append(p.ReqHeaders[key], value)
}
}
func WsDialerOption(dialer *websocket.Dialer) WsParameterOption {
return func(p *WsParameter) {
p.Dialer = dialer
}
}
func WsAutoReconnectOption(autoReconnect bool) WsParameterOption {
return func(p *WsParameter) {
p.AutoReconnect = autoReconnect
}
}
func WsReSubscribeOption(reSubscribe bool) WsParameterOption {
return func(p *WsParameter) {
p.ReSubscribe = reSubscribe
}
}
func WsDumpOption(dump bool) WsParameterOption {
return func(p *WsParameter) {
p.IsDump = dump
}
}
func WsMessageHandleFuncOption(f func([]byte) error) WsParameterOption {
return func(p *WsParameter) {
p.MessageHandleFunc = f
}
}
func WsReSubscribeFuncOption(f func(ws *WsConn) error) WsParameterOption {
return func(p *WsParameter) {
p.ReSubscribeFunc = f
}
}
func WsErrorHandleFuncOption(f func(err error)) WsParameterOption {
return func(p *WsParameter) {
p.ErrorHandleFunc = f
}
}