-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsClient.go
74 lines (56 loc) · 2.25 KB
/
wsClient.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
package bybit
import (
"github.com/yasseldg/bybit/constants"
"github.com/yasseldg/bybit/ws/common"
"github.com/yasseldg/go-simple/logs/sLog"
)
// ws
type BybitWsClient struct {
WsClient *common.WsClient
}
func NewWsClient(channel constants.Channel, key, secret string) *BybitWsClient {
wsc := new(common.WsClient)
wsc.Init(channel, key, secret, listener, errorListener)
return &BybitWsClient{wsc}
}
func listener(message string) {
sLog.Debug("WebSocket message:" + message)
}
func errorListener(message string) {
sLog.Error(message)
}
func WscPublicSpot() *BybitWsClient {
return NewWsClient(constants.Channel_PublicSpot, "", "")
}
func WscPublicLinear() *BybitWsClient {
return NewWsClient(constants.Channel_PublicLinear, "", "")
}
func WscPublicInverse() *BybitWsClient {
return NewWsClient(constants.Channel_PublicInverse, "", "")
}
func WscPublicOption() *BybitWsClient {
return NewWsClient(constants.Channel_PublicOption, "", "")
}
func WscPrivate(key, secret string) *BybitWsClient {
return NewWsClient(constants.Channel_Private, key, secret)
}
type UnsuscribeFunc func()
func (bws *BybitWsClient) Subscribe(listener common.OnReceive, topics ...constants.SubscribeTopic) UnsuscribeFunc {
bws.WsClient.Subscribe(topics, listener)
return func() { bws.WsClient.UnSubscribe(topics) }
}
func (bws *BybitWsClient) SubscribeOrderbook(listener common.OnReceive, symbol constants.Symbol, interval constants.Interval) UnsuscribeFunc {
return bws.Subscribe(listener, constants.GetTopicOrderbook(symbol, interval))
}
func (bws *BybitWsClient) SubscribeKline(listener common.OnReceive, symbol constants.Symbol, interval constants.Interval) UnsuscribeFunc {
return bws.Subscribe(listener, constants.GetTopicKline(symbol, interval))
}
func (bws *BybitWsClient) SubscribeTrade(listener common.OnReceive, symbol constants.Symbol) UnsuscribeFunc {
return bws.Subscribe(listener, constants.GetTopicTrade(symbol))
}
func (bws *BybitWsClient) SubscribeTickers(listener common.OnReceive, symbol constants.Symbol) UnsuscribeFunc {
return bws.Subscribe(listener, constants.GetTopicTickers(symbol))
}
func (bws *BybitWsClient) SubscribeLiquidation(listener common.OnReceive, symbol constants.Symbol) UnsuscribeFunc {
return bws.Subscribe(listener, constants.GetTopicLiquidation(symbol))
}