forked from HuobiRDCenter/huobi_Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribeorderwebsocketv1client.go
45 lines (38 loc) · 1.59 KB
/
subscribeorderwebsocketv1client.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
package orderwebsocketclient
import (
"encoding/json"
"fmt"
"github.com/model-collapse/huobi_golang/pkg/client/websocketclientbase"
"github.com/model-collapse/huobi_golang/pkg/model/order"
)
// Responsible to handle order subscription from WebSocket
// This need authentication version 1
type SubscribeOrderWebSocketV1Client struct {
websocketclientbase.WebSocketV1ClientBase
}
// Initializer
func (p *SubscribeOrderWebSocketV1Client) Init(accessKey string, secretKey string, host string) *SubscribeOrderWebSocketV1Client {
p.WebSocketV1ClientBase.Init(accessKey, secretKey, host)
return p
}
// Set callback handler
func (p *SubscribeOrderWebSocketV1Client) SetHandler(
authHandler websocketclientbase.AuthenticationV1ResponseHandler,
responseHandler websocketclientbase.ResponseHandler) {
p.WebSocketV1ClientBase.SetHandler(authHandler, p.handleMessage, responseHandler)
}
// Subscribe all order updates of the current account
func (p *SubscribeOrderWebSocketV1Client) Subscribe(symbol string, clientId string) error {
sub := fmt.Sprintf("{ \"op\":\"sub\", \"topic\":\"orders.%s.update\", \"cid\": \"%s\"}", symbol, clientId)
return p.Send(sub)
}
// Unsubscribe order updates
func (p *SubscribeOrderWebSocketV1Client) UnSubscribe(symbol string, clientId string) error {
unsub := fmt.Sprintf("{ \"op\":\"unsub\", \"topic\":\"orders.%s.update\", \"cid\": \"%s\"}", symbol, clientId)
return p.Send(unsub)
}
func (p *SubscribeOrderWebSocketV1Client) handleMessage(msg string) (interface{}, error) {
result := order.SubscribeOrderV1Response{}
err := json.Unmarshal([]byte(msg), &result)
return result, err
}