-
Notifications
You must be signed in to change notification settings - Fork 34
/
diffBookDepthWebsocket.go
116 lines (100 loc) · 3.33 KB
/
diffBookDepthWebsocket.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
package futuresusdt
import (
"encoding/json"
"github.com/dirname/binance"
"github.com/dirname/binance/config"
logger "github.com/dirname/binance/logging"
"github.com/dirname/binance/model"
)
// FuturesDiffBookDepthWebsocketClient responsible to handle market price data from websocket
type FuturesDiffBookDepthWebsocketClient struct {
binance.WebsocketClient
}
// NewUSDFuturesDiffBookDepthWebsocketClient Factory function
func NewUSDFuturesDiffBookDepthWebsocketClient(streams ...string) *FuturesDiffBookDepthWebsocketClient {
c := new(FuturesDiffBookDepthWebsocketClient)
c.WebsocketClient.Init(config.USDFuturesWssHost, streams...)
return c
}
// Subscribe subscribe market price data
func (u *FuturesDiffBookDepthWebsocketClient) Subscribe(id uint, params ...string) {
req := model.WebsocketCommon{
Method: "SUBSCRIBE",
Params: params,
ID: id,
}
u.WebsocketClient.SendJSON(req)
logger.Info("Websocket subscribed, params: %s id: %d", params, id)
}
// Unsubscribe unsubscribe market price data
func (u *FuturesDiffBookDepthWebsocketClient) Unsubscribe(id uint, params ...string) {
req := model.WebsocketCommon{
Method: "UNSUBSCRIBE",
Params: params,
ID: id,
}
u.WebsocketClient.SendJSON(req)
logger.Info("Websocket unsubscribed, params: %s id: %d", params, id)
}
// SetCombined set combined, it is true when stream's length is greater than one, and false if stream's length is equal to one
func (u *FuturesDiffBookDepthWebsocketClient) SetCombined(b bool, id uint) {
req := model.WebsocketCommon{
Method: "SET_PROPERTY",
Params: []interface{}{"combined", b},
ID: id,
}
u.WebsocketClient.SendJSON(req)
logger.Info("Websocket set combined, params: %v", req)
}
// GetCombined get combined
func (u *FuturesDiffBookDepthWebsocketClient) GetCombined(id uint) {
req := model.WebsocketCommon{
Method: "GET_PROPERTY",
Params: []interface{}{"combined"},
ID: id,
}
u.WebsocketClient.SendJSON(req)
logger.Info("Websocket set combined, params: %v", req)
}
// GetSubscribe get subscribed list
func (u *FuturesDiffBookDepthWebsocketClient) GetSubscribe(id uint) {
req := model.WebsocketCommon{
Method: "LIST_SUBSCRIPTIONS",
Params: nil,
ID: id,
}
u.WebsocketClient.SendJSON(req)
logger.Info("Websocket get subscribed, params: %v", req)
}
// SetHandler set callback handler
func (u *FuturesDiffBookDepthWebsocketClient) SetHandler(connectHandler binance.ConnectedHandler, responseHandler binance.ResponseHandler) {
u.WebsocketClient.SetConnectedHandler(connectHandler)
u.WebsocketClient.SetResponseHandler(responseHandler)
u.WebsocketClient.SetMessageHandler(u.handleMessage)
}
func (u *FuturesDiffBookDepthWebsocketClient) handleMessage(msg []byte) (interface{}, error) {
var parser map[string]interface{}
var err error
err = json.Unmarshal(msg, &parser)
if _, ok := parser["stream"]; ok {
result := DiffBookDepthCombinedResponse{}
err = json.Unmarshal(msg, &result)
return result, err
}
if _, ok := parser["result"]; ok {
result := model.WebsocketCommonResponse{}
err = json.Unmarshal(msg, &result)
return result, err
}
if _, ok := parser["e"]; ok {
result := DiffBookDepthResponse{}
err = json.Unmarshal(msg, &result)
return result, err
}
if _, ok := parser["code"]; ok {
result := model.WebsocketErrorResponse{}
err = json.Unmarshal(msg, &result)
return result, err
}
return parser, err
}