forked from HuobiRDCenter/huobi_Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marketbypricewebsocketclient.go
83 lines (63 loc) · 2.83 KB
/
marketbypricewebsocketclient.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
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 MBP data from WebSocket
type MarketByPriceWebSocketClient struct {
websocketclientbase.WebSocketClientBase
}
// Initializer
func (p *MarketByPriceWebSocketClient) Init(host string) *MarketByPriceWebSocketClient {
p.WebSocketClientBase.Init(host)
return p
}
// Set callback handler
func (p *MarketByPriceWebSocketClient) SetHandler(
connectedHandler websocketclientbase.ConnectedHandler,
responseHandler websocketclientbase.ResponseHandler) {
p.WebSocketClientBase.SetHandler(connectedHandler, p.handleMessage, responseHandler)
}
// Request full Market By Price order book
func (p *MarketByPriceWebSocketClient) Request(symbol string, clientId string) {
topic := fmt.Sprintf("market.%s.mbp.150", symbol)
req := fmt.Sprintf("{\"req\": \"%s\",\"id\": \"%s\" }", topic, clientId)
p.WebSocketClientBase.Send(req)
applogger.Info("WebSocket requested, topic=%s, clientId=%s", topic, clientId)
}
// Subscribe incremental update of Market By Price order book
func (p *MarketByPriceWebSocketClient) Subscribe(symbol string, clientId string) {
topic := fmt.Sprintf("market.%s.mbp.150", symbol)
sub := fmt.Sprintf("{\"sub\": \"%s\",\"id\": \"%s\" }", topic, clientId)
p.WebSocketClientBase.Send(sub)
applogger.Info("WebSocket subscribed, topic=%s, clientId=%s", topic, clientId)
}
// Subscribe full Market By Price order book
func (p *MarketByPriceWebSocketClient) SubscribeFull(symbol string, level int, clientId string) {
topic := fmt.Sprintf("market.%s.mbp.refresh.%d", symbol, level)
sub := fmt.Sprintf("{\"sub\": \"%s\",\"id\": \"%s\" }", topic, clientId)
p.Send(sub)
applogger.Info("WebSocket subscribed, topic=%s, clientId=%s", topic, clientId)
}
// Unsubscribe update of Market By Price order book
func (p *MarketByPriceWebSocketClient) UnSubscribe(symbol string, clientId string) {
topic := fmt.Sprintf("market.%s.mbp.150", symbol)
unsub := fmt.Sprintf("{\"unsub\": \"%s\",\"id\": \"%s\" }", topic, clientId)
p.Send(unsub)
applogger.Info("WebSocket unsubscribed, topic=%s, clientId=%s", topic, clientId)
}
// Unsubscribe full Market By Price order book
func (p *MarketByPriceWebSocketClient) UnSubscribeFull(symbol string, level int, clientId string) {
topic := fmt.Sprintf("market.%s.mbp.refresh.%d", symbol, level)
unsub := fmt.Sprintf("{\"unsub\": \"%s\",\"id\": \"%s\" }", topic, clientId)
p.Send(unsub)
applogger.Info("WebSocket unsubscribed, topic=%s, clientId=%s", topic, clientId)
}
func (p *MarketByPriceWebSocketClient) handleMessage(msg string) (interface{}, error) {
result := market.SubscribeMarketByPriceResponse{}
err := json.Unmarshal([]byte(msg), &result)
return result, err
}