-
Notifications
You must be signed in to change notification settings - Fork 6
/
ws_public_tiny.go
127 lines (112 loc) · 3.01 KB
/
ws_public_tiny.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
package spot
import (
"fmt"
"github.com/ginarea/gobybit/bybitv2/transport"
"github.com/msw-x/moon/ulog"
)
type WsPublicTiny struct {
log *ulog.Log
ws *transport.WsClient
}
func NewWsPublicTiny(url string) *WsPublicTiny {
ws := transport.NewWsClient(url)
return &WsPublicTiny{
log: ulog.Empty(),
ws: ws,
}
}
func (this *WsPublicTiny) Shutdown() {
this.log.Debug("shutdown")
this.ws.Shutdown()
}
func (this *WsPublicTiny) Conf() *transport.WsConf {
return this.ws.Conf()
}
func (this *WsPublicTiny) WithLog(log *ulog.Log) *WsPublicTiny {
this.ws.WithLog(log)
return this
}
func (this *WsPublicTiny) WithProxy(proxy string) *WsPublicTiny {
this.ws.WithProxy(proxy)
return this
}
func (this *WsPublicTiny) Connected() bool {
return this.ws.Connected()
}
func (this *WsPublicTiny) Run() {
this.log.Debug("run")
this.ws.SetOnMessage(this.processMessage)
this.ws.Run()
}
func (this *WsPublicTiny) SetOnConnected(onConnected func()) {
this.ws.SetOnConnected(onConnected)
}
func (this *WsPublicTiny) Subscribe(topic TopicName, symbol string) bool {
this.log.Infof("subscribe: topic[%s] symbol[%s] ", symbol, topic)
return this.ws.Send(Topic{
Name: topic,
Event: TopicEventSub,
Params: TopicParams{
Symbol: symbol,
},
})
}
func (this *WsPublicTiny) Unsubscribe(topic TopicName, symbol string) bool {
this.log.Infof("unsubscribe: topic[%s] symbol[%s]", symbol, topic)
return this.ws.Send(Topic{
Name: topic,
Event: TopicEventCancel,
Params: TopicParams{
Symbol: symbol,
},
})
}
func (this *WsPublicTiny) processMessage(name string, msg []byte) {
switch name {
case "pong":
v := transport.JsonUnmarshal[struct {
Pong int `json:"pong"`
}](msg)
this.log.Debug("pong:", v.Pong)
case "code":
v := transport.JsonUnmarshal[struct {
Code string `json:"code"`
Description string `json:"desc"`
}](msg)
this.log.Warningf("code[%s]: %s", v.Code, v.Description)
case "topic":
v := transport.JsonUnmarshal[TopicSubscribtion](msg)
if v.HasCode() {
//success := v.Ok()
this.log.Infof("topic %s (%s) subscribtion: %s (%s)", v.Topic.Name, v.Params.Symbol, v.Message, v.Code)
} else {
this.processTopic(msg)
}
default:
panic("unknown message type")
}
}
func (this *WsPublicTiny) processTopic(msg []byte) {
v := transport.JsonUnmarshal[TopicNotification[any]](msg)
var data any
switch v.Topic {
case TopicDepth:
v := transport.JsonUnmarshal[TopicNotification[TopicDataDepth]](msg)
data = v.Data
case TopicKline:
v := transport.JsonUnmarshal[TopicNotification[TopicDataKline]](msg)
data = v.Data
case TopicTrade:
v := transport.JsonUnmarshal[TopicNotification[TopicDataTrade]](msg)
data = v.Data
case TopicBookTicker:
v := transport.JsonUnmarshal[TopicNotification[TopicDataBookTicker]](msg)
data = v.Data
case TopicRealtimes:
v := transport.JsonUnmarshal[TopicNotification[TopicDataRealtimes]](msg)
data = v.Data
default:
panic(fmt.Sprintf("unknown topic name: %s", v.Topic))
}
this.log.Infof("topic %s (%s) notify: %+v", v.Topic, v.Params.Symbol, data)
}