/
client.go
317 lines (267 loc) · 6.39 KB
/
client.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package okex
import (
"bytes"
"compress/flate"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/blockcdn-go/exchange-sdk-go/config"
"github.com/google/uuid"
"github.com/gorilla/websocket"
"github.com/gotoxu/log/core"
jsoniter "github.com/json-iterator/go"
"github.com/json-iterator/go/extra"
)
// WSSClient 提供okex API调用的客户端
type WSSClient struct {
config config.Config
conns map[string]*websocket.Conn
logger core.Logger
closed bool
closeMu sync.Mutex
once sync.Once
shouldQuit chan struct{}
retry chan string
done chan struct{}
}
// NewWSSClient 创建一个新的Websocket client
func NewWSSClient(config *config.Config) *WSSClient {
cfg := defaultConfig()
if config != nil {
cfg.MergeIn(config)
}
return &WSSClient{
config: *cfg,
conns: make(map[string]*websocket.Conn),
shouldQuit: make(chan struct{}),
retry: make(chan string),
done: make(chan struct{}),
}
}
// SetLogger 设置日志器
func (c *WSSClient) SetLogger(logger core.Logger) {
c.logger = logger
}
// QuerySpot 负责订阅现货行情数据
func (c *WSSClient) QuerySpot() (<-chan []byte, error) {
cid, conn, err := c.connect("/websocket")
if err != nil {
return nil, err
}
err = c.subscribeSpot(conn)
if err != nil {
c.Close()
return nil, err
}
result := make(chan []byte)
go c.start(cid, "/websocket", result)
return result, nil
}
func (c *WSSClient) log(level core.Level, v ...interface{}) {
if c.logger != nil {
c.logger.Log(level, v...)
}
}
func (c *WSSClient) logf(level core.Level, format string, v ...interface{}) {
if c.logger != nil {
c.logger.Logf(level, format, v...)
}
}
func (c *WSSClient) logln(level core.Level, v ...interface{}) {
if c.logger != nil {
c.logger.Logln(level, v...)
}
}
func (c *WSSClient) subscribeSpot(conn *websocket.Conn) error {
for _, v := range events {
err := conn.WriteJSON(v)
if err != nil {
return err
}
}
return nil
}
func (c *WSSClient) start(cid, path string, msgCh chan<- []byte) {
go c.query(cid, msgCh)
ticker := time.NewTicker(*c.config.PingDuration)
defer ticker.Stop()
for {
select {
case <-ticker.C:
conn := c.conns[cid]
conn.WriteMessage(websocket.TextMessage, []byte("{'event':'ping'}"))
case cid := <-c.retry:
delete(c.conns, cid)
c.reconnect(path, msgCh)
case <-c.shouldQuit:
c.shutdown()
return
}
}
}
func (c *WSSClient) shutdown() {
c.closeMu.Lock()
c.closed = true
c.closeMu.Unlock()
for _, conn := range c.conns {
conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
conn.Close()
}
close(c.done)
}
func (c *WSSClient) reconnect(path string, msgCh chan<- []byte) {
cid, conn, err := c.connect(path)
if err != nil {
return
}
err = c.subscribeSpot(conn)
if err != nil {
c.closeMu.Lock()
defer c.closeMu.Unlock()
if !c.closed {
c.retry <- cid
}
return
}
go c.query(cid, msgCh)
}
func (c *WSSClient) query(cid string, msgCh chan<- []byte) {
conn, ok := c.conns[cid]
if !ok {
return
}
for {
_, msg, err := conn.ReadMessage()
if err != nil {
c.closeMu.Lock()
defer c.closeMu.Unlock()
if !c.closed {
c.retry <- cid
}
return
}
if strings.Contains(string(msg), "pong") {
continue
}
buf := bytes.NewBuffer(msg)
z := flate.NewReader(buf)
m, _ := ioutil.ReadAll(z)
// 作为sdk开发者来说,我们并不知晓调用方需要哪些数据,因此这里不做过滤,而在调用方再做过滤
// 从代码致性效率来说,这里反序列化一次,然后将消息传给调用方,调用方仍然需要反序列化,因此直接在调用方做反序列化并过滤
// 而sdk中不做处理
// var subrsp [1]struct {
// Data struct {
// Result string `json:"result"`
// } `json:"data"`
// }
// if e := json.Unmarshal(m, &subrsp); e != nil {
// // 订阅请求的回复,不包含数据,直接忽略
// log.Print("ignore subscribe respone.")
// continue
// }
msgCh <- m
}
}
// Close 向服务端发起关闭操作
func (c *WSSClient) Close() {
if c.conns == nil {
return
}
close(c.shouldQuit)
select {
case <-c.done:
case <-time.After(time.Second):
}
}
func (c *WSSClient) connect(path string) (string, *websocket.Conn, error) {
u := url.URL{Scheme: "wss", Host: *c.config.WSSHost, Path: path}
conn, _, err := c.config.WSSDialer.Dial(u.String(), nil)
if err == nil {
u := uuid.New().String()
c.conns[u] = conn
return u, conn, nil
}
if err == websocket.ErrBadHandshake {
return "", nil, err
}
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
conn, _, err := c.config.WSSDialer.Dial(u.String(), nil)
if err == nil {
u := uuid.New().String()
c.conns[u] = conn
return u, conn, nil
}
case <-c.shouldQuit:
return "", nil, errors.New("Connection is closing")
}
}
}
// Client ...
type Client struct {
config config.Config
}
// NewClient 创建一个新的client
func NewClient(config *config.Config) *Client {
cfg := defaultConfig()
if config != nil {
cfg.MergeIn(config)
}
return &Client{config: *cfg}
}
func (c *Client) doHTTP(method, path string,
reqarg interface{}, out interface{}, extraheader map[string]string) error {
url := "http://"
if *c.config.UseSSL {
url = "https://"
}
url += *c.config.RESTHost + path
arg := ""
if method == "POST" {
bytesParams, _ := json.Marshal(reqarg)
arg = string(bytesParams)
}
req, e := http.NewRequest(method, url, strings.NewReader(arg))
if e != nil {
return e
}
if method == "GET" {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
} else {
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("Accept-Language", "zh-cn")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) "+
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36")
for k, v := range extraheader {
req.Header.Set(k, v)
}
resp, re := c.config.HTTPClient.Do(req)
if e != nil {
return re
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("请求失败,响应码:%d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
fmt.Printf("okex http message:%s\n", string(body))
extra.RegisterFuzzyDecoders()
err = jsoniter.Unmarshal(body, out)
if err != nil {
return err
}
return nil
}