-
Notifications
You must be signed in to change notification settings - Fork 264
/
options.go
161 lines (136 loc) · 3.93 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
152
153
154
155
156
157
158
159
160
161
package client
import (
"time"
wkproto "github.com/WuKongIM/WuKongIMGoProto"
)
// Options Options
type Options struct {
ProtoVersion uint8 // 协议版本
UID string // 用户uid
Token string // 连接IM的token
AutoReconn bool //是否开启自动重连
DefaultBufSize int // The size of the bufio reader/writer on top of the socket.
// ReconnectBufSize is the size of the backing bufio during reconnect.
// Once this has been exhausted publish operations will return an error.
// Defaults to 8388608 bytes (8MB).
ReconnectBufSize int
// FlusherTimeout is the maximum time to wait for write operations
// to the underlying connection to complete (including the flusher loop).
FlusherTimeout time.Duration
// Timeout sets the timeout for a Dial operation on a connection.
Timeout time.Duration
PingInterval time.Duration
MaxPingCount int // 最大ping的次数
// ReconnectJitter sets the upper bound for a random delay added to
// ReconnectWait during a reconnect when no TLS is used.
ReconnectJitter time.Duration
// ReconnectWait sets the time to backoff after attempting a reconnect
// to a server that we were already connected to previously.
ReconnectWait time.Duration
}
// NewOptions 创建默认配置
func NewOptions() *Options {
return &Options{
ProtoVersion: wkproto.LatestVersion,
AutoReconn: false,
DefaultBufSize: 1024 * 1024 * 3,
ReconnectBufSize: 8 * 1024 * 1024,
Timeout: 2 * time.Second,
PingInterval: 2 * time.Minute,
MaxPingCount: 2,
ReconnectJitter: 100 * time.Millisecond,
ReconnectWait: 2 * time.Second,
}
}
// Option 参数项
type Option func(*Options) error
// WithProtoVersion 设置协议版本
func WithProtoVersion(version uint8) Option {
return func(opts *Options) error {
opts.ProtoVersion = version
return nil
}
}
// WithUID 用户UID
func WithUID(uid string) Option {
return func(opts *Options) error {
opts.UID = uid
return nil
}
}
// WithToken 用户token
func WithToken(token string) Option {
return func(opts *Options) error {
opts.Token = token
return nil
}
}
// WithAutoReconn WithAutoReconn
func WithAutoReconn(autoReconn bool) Option {
return func(opts *Options) error {
opts.AutoReconn = autoReconn
return nil
}
}
// SendOptions SendOptions
type SendOptions struct {
NoPersist bool // 是否不存储 默认 false
SyncOnce bool // 是否同步一次(写模式) 默认 false
Flush bool // 是否io flush 默认true
RedDot bool // 是否显示红点 默认true
NoEncrypt bool // 是否不需要加密
ClientMsgNo string
}
// NewSendOptions NewSendOptions
func NewSendOptions() *SendOptions {
return &SendOptions{
NoPersist: false,
SyncOnce: false,
Flush: true,
RedDot: true,
}
}
// SendOption 参数项
type SendOption func(*SendOptions) error
// SendOptionWithNoPersist 是否不存储
func SendOptionWithNoPersist(noPersist bool) SendOption {
return func(opts *SendOptions) error {
opts.NoPersist = noPersist
return nil
}
}
// SendOptionWithSyncOnce 是否只同步一次(写模式)
func SendOptionWithSyncOnce(syncOnce bool) SendOption {
return func(opts *SendOptions) error {
opts.SyncOnce = syncOnce
return nil
}
}
// SendOptionWithFlush 是否 io flush
func SendOptionWithFlush(flush bool) SendOption {
return func(opts *SendOptions) error {
opts.Flush = flush
return nil
}
}
// SendOptionWithRedDot 是否显示红点
func SendOptionWithRedDot(redDot bool) SendOption {
return func(opts *SendOptions) error {
opts.RedDot = redDot
return nil
}
}
// SendOptionWithClientMsgNo 是否显示红点
func SendOptionWithClientMsgNo(clientMsgNo string) SendOption {
return func(opts *SendOptions) error {
opts.ClientMsgNo = clientMsgNo
return nil
}
}
// SendOptionWithNoEncrypt 是否不需要加密
func SendOptionWithNoEncrypt(noEncrypt bool) SendOption {
return func(opts *SendOptions) error {
opts.NoEncrypt = noEncrypt
return nil
}
}