forked from HuobiRDCenter/huobi_Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bestbidofferwebsocketclient.go
53 lines (42 loc) · 1.7 KB
/
bestbidofferwebsocketclient.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
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 BBO data from WebSocket
type BestBidOfferWebSocketClient struct {
websocketclientbase.WebSocketClientBase
}
// Initializer
func (p *BestBidOfferWebSocketClient) Init(host string) *BestBidOfferWebSocketClient {
p.WebSocketClientBase.Init(host)
return p
}
// Set callback handler
func (p *BestBidOfferWebSocketClient) SetHandler(
connectedHandler websocketclientbase.ConnectedHandler,
responseHandler websocketclientbase.ResponseHandler) {
p.WebSocketClientBase.SetHandler(connectedHandler, p.handleMessage, responseHandler)
}
// Subscribe latest market by price order book in snapshot mode at 1-second interval.
func (p *BestBidOfferWebSocketClient) Subscribe(symbol string, clientId string) {
topic := fmt.Sprintf("market.%s.bbo", symbol)
sub := fmt.Sprintf("{\"sub\": \"%s\", \"id\": \"%s\"}", topic, clientId)
p.Send(sub)
applogger.Info("WebSocket subscribed, topic=%s, clientId=%s", topic, clientId)
}
// Unsubscribe market by price order book
func (p *BestBidOfferWebSocketClient) UnSubscribe(symbol string, clientId string) {
topic := fmt.Sprintf("market.%s.bbo", symbol)
unsub := fmt.Sprintf("{\"unsub\": \"%s\", \"id\": \"%s\" }", topic, clientId)
p.Send(unsub)
applogger.Info("WebSocket unsubscribed, topic=%s, clientId=%s", topic, clientId)
}
func (p *BestBidOfferWebSocketClient) handleMessage(msg string) (interface{}, error) {
result := market.SubscribeBestBidOfferResponse{}
err := json.Unmarshal([]byte(msg), &result)
return result, err
}