You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.8 KiB

package bawssclient
import (
"encoding/json"
"fmt"
"github.com/shopspring/decimal"
"strings"
"wss-pool/dictionary"
"wss-pool/logging/applogger"
"wss-pool/pkg/client/bawebsocketclientbase"
"wss-pool/pkg/model/market"
)
type BaKLineParam struct {
Method string `json:"method"`
Params []string `json:"params"`
ID int64 `json:"id"`
}
type BaKLineResponseK struct {
T int64 `json:"t"`
Ts int64 `json:"T"`
S string `json:"s"`
I string `json:"i"`
F int64 `json:"f"`
L interface{} `json:"L"`
O string `json:"o"`
C string `json:"c"`
H string `json:"h"`
l string `json:"l"`
V string `json:"v"`
N int `json:"n"`
X bool `json:"x"`
Vs string `json:"V"`
Q string `json:"Q"`
B string `json:"B"`
}
type BaKLineResponse struct {
E string `json:"e"` //事件类型
Es int64 `json:"E"` //事件时间
S string `json:"s"` //交易对
K BaKLineResponseK `json:"k"`
}
// Responsible to handle candlestick data from WebSocket
type CandlestickWebSocketClient struct {
bawebsocketclientbase.WebSocketClientBase
}
// Initializer
func (p *CandlestickWebSocketClient) Init(host string) *CandlestickWebSocketClient {
p.WebSocketClientBase.Init(host)
return p
}
// Request the full candlestick data according to specified criteria
//func (p *CandlestickWebSocketClient) Request(symbol string) {
// topic := fmt.Sprintf("market.%s.kline.%s", symbol, period)
// req := fmt.Sprintf("{\"req\": \"%s\", \"from\":%d, \"to\":%d, \"id\": \"%s\" }", topic, from, to, clientId)
//
// p.Send(req)
//
// applogger.Info("WebSocket requested, topic=%s, clientId=%s", topic, clientId)
//}
// Set callback handler
func (p *CandlestickWebSocketClient) SetHandler(
connectedHandler bawebsocketclientbase.ConnectedHandler,
responseHandler bawebsocketclientbase.ResponseHandler) {
p.WebSocketClientBase.SetHandler(connectedHandler, p.handleMessage, responseHandler)
}
// Request the full candlestick data according to specified criteria
// Subscribe candlestick data
func (p *CandlestickWebSocketClient) Subscribe(symbol string) {
topis := make([]string, 0)
for _, v := range dictionary.BaTimeCycle {
topis = append(topis, fmt.Sprintf("%susdt@kline_%s", symbol, v))
}
sub := BaKLineParam{
Method: "SUBSCRIBE",
Params: topis,
ID: bawebsocketclientbase.Randomnumber(),
}
data, _ := json.Marshal(sub)
p.Send(data)
applogger.Info("WebSocket subscribed, clientId=%s", sub.ID)
}
// Unsubscribe candlestick data
func (p *CandlestickWebSocketClient) UnSubscribe(symbol string) {
topis := make([]string, 0)
for _, v := range dictionary.BaTimeCycle {
topis = append(topis, fmt.Sprintf("%susdt@%s", symbol, v))
}
sub := BaKLineParam{
Method: "UNSUBSCRIBE",
Params: topis,
ID: bawebsocketclientbase.Randomnumber(),
}
data, _ := json.Marshal(sub)
p.Send(data)
applogger.Info("WebSocket unsubscribed, clientId=%s", sub.ID)
}
func (p *CandlestickWebSocketClient) handleMessage(msg string) (interface{}, error) {
baRes := BaKLineResponse{}
err := json.Unmarshal([]byte(msg), &baRes)
//转火币数据结构
amount, _ := decimal.NewFromString(baRes.K.V)
open, _ := decimal.NewFromString(baRes.K.O)
close, _ := decimal.NewFromString(baRes.K.C)
low, _ := decimal.NewFromString(baRes.K.l)
high, _ := decimal.NewFromString(baRes.K.H)
vol, _ := decimal.NewFromString(baRes.K.Q)
result := market.SubscribeCandlestickResponse{
Channel: fmt.Sprintf("market.%s.kline.%s", strings.ToLower(baRes.S), dictionary.BaToBaMap[baRes.K.I]),
Timestamp: baRes.Es,
Tick: &market.Tick{
Id: baRes.Es,
Amount: amount,
Count: baRes.K.N,
Open: open,
Close: close,
Low: low,
High: high,
Vol: vol,
IsBa: bawebsocketclientbase.BinAnce,
},
}
return result, err
}