forked from hirokisan/bybit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v5_ws_public.go
259 lines (222 loc) · 5.79 KB
/
v5_ws_public.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
package bybit
import (
"context"
"encoding/json"
"errors"
"log"
"os"
"os/signal"
"strings"
"sync"
"time"
"github.com/gorilla/websocket"
)
// V5WebsocketPublicServiceI :
type V5WebsocketPublicServiceI interface {
Start(context.Context, ErrHandler) error
Run() error
Ping() error
Close() error
SubscribeOrderBook(
V5WebsocketPublicOrderBookParamKey,
func(V5WebsocketPublicOrderBookResponse) error,
) (func() error, error)
SubscribeKline(
V5WebsocketPublicKlineParamKey,
func(V5WebsocketPublicKlineResponse) error,
) (func() error, error)
SubscribeTicker(
V5WebsocketPublicTickerParamKey,
func(V5WebsocketPublicTickerResponse) error,
) (func() error, error)
}
// V5WebsocketPublicService :
type V5WebsocketPublicService struct {
client *WebSocketClient
connection *websocket.Conn
mu sync.Mutex
paramOrderBookMap map[V5WebsocketPublicOrderBookParamKey]func(V5WebsocketPublicOrderBookResponse) error
paramKlineMap map[V5WebsocketPublicKlineParamKey]func(V5WebsocketPublicKlineResponse) error
paramTickerMap map[V5WebsocketPublicTickerParamKey]func(V5WebsocketPublicTickerResponse) error
}
const (
// V5WebsocketPublicPath :
V5WebsocketPublicPath = "/v5/public"
)
// V5WebsocketPublicPathFor :
func V5WebsocketPublicPathFor(category CategoryV5) string {
return V5WebsocketPublicPath + "/" + string(category)
}
// V5WebsocketPublicTopic :
type V5WebsocketPublicTopic string
const (
// V5WebsocketPublicTopicOrderBook :
V5WebsocketPublicTopicOrderBook = V5WebsocketPublicTopic("orderbook")
// V5WebsocketPublicTopicKline :
V5WebsocketPublicTopicKline = V5WebsocketPublicTopic("kline")
// V5WebsocketPublicTopicTicker :
V5WebsocketPublicTopicTicker = V5WebsocketPublicTopic("tickers")
)
func (t V5WebsocketPublicTopic) String() string {
return string(t)
}
// judgeTopic :
func (s *V5WebsocketPublicService) judgeTopic(respBody []byte) (V5WebsocketPublicTopic, error) {
parsedData := map[string]interface{}{}
if err := json.Unmarshal(respBody, &parsedData); err != nil {
return "", err
}
if topic, ok := parsedData["topic"].(string); ok {
switch {
case strings.Contains(topic, V5WebsocketPublicTopicOrderBook.String()):
return V5WebsocketPublicTopicOrderBook, nil
case strings.Contains(topic, V5WebsocketPublicTopicKline.String()):
return V5WebsocketPublicTopicKline, nil
case strings.Contains(topic, V5WebsocketPublicTopicTicker.String()):
return V5WebsocketPublicTopicTicker, nil
}
}
return "", nil
}
// UnmarshalJSON :
func (r *V5WebsocketPublicTickerData) UnmarshalJSON(data []byte) error {
var res struct {
Bid1Price string `json:"bid1Price"`
Gamma string `json:"gamma"`
}
if err := json.Unmarshal(data, &res); err != nil {
return err
}
if res.Bid1Price != "" {
return json.Unmarshal(data, &r.LinearInverse)
}
if res.Gamma != "" {
return json.Unmarshal(data, &r.Option)
}
return json.Unmarshal(data, &r.Spot)
}
// parseResponse :
func (s *V5WebsocketPublicService) parseResponse(respBody []byte, response interface{}) error {
if err := json.Unmarshal(respBody, &response); err != nil {
return err
}
return nil
}
// Start :
func (s *V5WebsocketPublicService) Start(ctx context.Context, errHandler ErrHandler) error {
done := make(chan struct{})
go func() {
defer close(done)
defer s.connection.Close()
_ = s.connection.SetReadDeadline(time.Now().Add(60 * time.Second))
s.connection.SetPongHandler(func(string) error {
_ = s.connection.SetReadDeadline(time.Now().Add(60 * time.Second))
return nil
})
for {
if err := s.Run(); err != nil {
if errHandler == nil {
return
}
errHandler(IsErrWebsocketClosed(err), err)
return
}
}
}()
ticker := time.NewTicker(20 * time.Second)
defer ticker.Stop()
ctx, stop := signal.NotifyContext(ctx, os.Interrupt)
defer stop()
for {
select {
case <-done:
return nil
case <-ticker.C:
if err := s.Ping(); err != nil {
return err
}
case <-ctx.Done():
log.Println("interrupt")
if err := s.Close(); err != nil {
return err
}
select {
case <-done:
case <-time.After(time.Second):
}
return nil
}
}
}
// Run :
func (s *V5WebsocketPublicService) Run() error {
_, message, err := s.connection.ReadMessage()
if err != nil {
return err
}
topic, err := s.judgeTopic(message)
if err != nil {
return err
}
switch topic {
case V5WebsocketPublicTopicOrderBook:
var resp V5WebsocketPublicOrderBookResponse
if err := s.parseResponse(message, &resp); err != nil {
return err
}
f, err := s.retrieveOrderBookFunc(resp.Key())
if err != nil {
return err
}
if err := f(resp); err != nil {
return err
}
case V5WebsocketPublicTopicKline:
var resp V5WebsocketPublicKlineResponse
if err := s.parseResponse(message, &resp); err != nil {
return err
}
f, err := s.retrieveKlineFunc(resp.Key())
if err != nil {
return err
}
if err := f(resp); err != nil {
return err
}
case V5WebsocketPublicTopicTicker:
var resp V5WebsocketPublicTickerResponse
if err := s.parseResponse(message, &resp); err != nil {
return err
}
f, err := s.retrieveTickerFunc(resp.Key())
if err != nil {
return err
}
if err := f(resp); err != nil {
return err
}
}
return nil
}
// Ping :
func (s *V5WebsocketPublicService) Ping() error {
if err := s.writeMessage(websocket.PingMessage, nil); err != nil {
return err
}
return nil
}
// Close :
func (s *V5WebsocketPublicService) Close() error {
if err := s.writeMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")); err != nil && !errors.Is(err, websocket.ErrCloseSent) {
return err
}
return nil
}
func (s *V5WebsocketPublicService) writeMessage(messageType int, body []byte) error {
s.mu.Lock()
defer s.mu.Unlock()
if err := s.connection.WriteMessage(messageType, body); err != nil {
return err
}
return nil
}