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.

63 lines
2.2 KiB

package marketwssclient
import (
"encoding/json"
"fmt"
"wss-pool/logging/applogger"
"wss-pool/pkg/client/hbwebsocketclientbase"
"wss-pool/pkg/model/market"
)
// Responsible to handle candlestick data from WebSocket
type ContractDepthSizeWebSocketClient struct {
hbwebsocketclientbase.WebSocketClientBase
}
// Initializer
func (p *ContractDepthSizeWebSocketClient) Init(host string) *ContractDepthSizeWebSocketClient {
p.WebSocketClientBase.Init(host)
return p
}
// Set callback handler
func (p *ContractDepthSizeWebSocketClient) SetHandler(
connectedHandler hbwebsocketclientbase.ConnectedHandler,
responseHandler hbwebsocketclientbase.ResponseHandler) {
p.WebSocketClientBase.SetHandler(connectedHandler, p.handleMessage, responseHandler)
}
// Request the full candlestick data according to specified criteria
func (p *ContractDepthSizeWebSocketClient) Request(symbol string, period string, from int64, to int64, clientId string) {
topic := fmt.Sprintf("market.%s.depth.size_%s.high_freq", 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)
}
// Subscribe candlestick data
func (p *ContractDepthSizeWebSocketClient) Subscribe(symbol string, period string, clientId string) {
topic := fmt.Sprintf("market.%s.depth.size_%s.high_freq", symbol, period)
sub := fmt.Sprintf("{\"sub\": \"%s\", \"data_type\": \"incremental\", \"id\": \"%s\"}", topic, clientId)
p.Send(sub)
applogger.Info("WebSocket subscribed, topic=%s, clientId=%s", topic, clientId)
}
// Unsubscribe candlestick data
func (p *ContractDepthSizeWebSocketClient) UnSubscribe(symbol string, period string, clientId string) {
topic := fmt.Sprintf("market.%s.depth.size_%s.high_freq", symbol, period)
unsub := fmt.Sprintf("{\"unsub\": \"%s\", \"data_type\": \"incremental\",\"id\": \"%s\" }", topic, clientId)
p.Send(unsub)
applogger.Info("WebSocket unsubscribed, topic=%s, clientId=%s", topic, clientId)
}
func (p *ContractDepthSizeWebSocketClient) handleMessage(msg string) (interface{}, error) {
result := market.SubscribeCtAddDepthResponse{}
err := json.Unmarshal([]byte(msg), &result)
return result, err
}