forked from HuobiRDCenter/huobi_Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
last24hcandlestickwebsocketclient.go
63 lines (49 loc) · 2.05 KB
/
last24hcandlestickwebsocketclient.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
package marketwebsocketclient
import (
"encoding/json"
"fmt"
"github.com/funtoy/huobi_golang/logging/applogger"
"github.com/funtoy/huobi_golang/pkg/client/websocketclientbase"
"github.com/funtoy/huobi_golang/pkg/model/market"
)
// Responsible to handle last 24h candlestick data from WebSocket
type Last24hCandlestickWebSocketClient struct {
websocketclientbase.WebSocketClientBase
}
// Initializer
func (p *Last24hCandlestickWebSocketClient) Init(host string) *Last24hCandlestickWebSocketClient {
p.WebSocketClientBase.Init(host)
return p
}
// Set callback handler
func (p *Last24hCandlestickWebSocketClient) SetHandler(
connectedHandler websocketclientbase.ConnectedHandler,
responseHandler websocketclientbase.ResponseHandler) {
p.WebSocketClientBase.SetHandler(connectedHandler, p.handleMessage, responseHandler)
}
// Request full candlestick data
func (p *Last24hCandlestickWebSocketClient) Request(symbol string, clientId string) {
topic := fmt.Sprintf("market.%s.detail", symbol)
req := fmt.Sprintf("{\"req\": \"%s\",\"id\": \"%s\" }", topic, clientId)
p.Send(req)
applogger.Info("WebSocket requested, topic=%s, clientId=%s", topic, clientId)
}
// Subscribe latest 24h market stats
func (p *Last24hCandlestickWebSocketClient) Subscribe(symbol string, clientId string) {
topic := fmt.Sprintf("market.%s.detail", symbol)
sub := fmt.Sprintf("{\"sub\": \"%s\",\"id\": \"%s\" }", topic, clientId)
p.Send(sub)
applogger.Info("WebSocket subscribed, topic=%s, clientId=%s", topic, clientId)
}
// Unsubscribe latest 24 market stats
func (p *Last24hCandlestickWebSocketClient) UnSubscribe(symbol string, clientId string) {
topic := fmt.Sprintf("market.%s.detail", symbol)
unsub := fmt.Sprintf("{\"unsub\": \"%s\",\"id\": \"%s\" }", symbol, clientId)
p.Send(unsub)
applogger.Info("WebSocket unsubscribed, topic=%s, clientId=%s", topic, clientId)
}
func (p *Last24hCandlestickWebSocketClient) handleMessage(msg string) (interface{}, error) {
result := market.SubscribeLast24hCandlestickResponse{}
err := json.Unmarshal([]byte(msg), &result)
return result, err
}