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.0 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 ContractBBOWebSocketClient struct {
hbwebsocketclientbase.WebSocketClientBase
}
// Initializer
func (p *ContractBBOWebSocketClient) Init(host string) *ContractBBOWebSocketClient {
p.WebSocketClientBase.Init(host)
return p
}
// Set callback handler
func (p *ContractBBOWebSocketClient) 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 *ContractBBOWebSocketClient) Request(symbol string, from int64, to int64, clientId string) {
topic := fmt.Sprintf("market.%s.bbo", symbol)
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 *ContractBBOWebSocketClient) 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 candlestick data
func (p *ContractBBOWebSocketClient) 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 *ContractBBOWebSocketClient) handleMessage(msg string) (interface{}, error) {
result := market.SubscribeCtBboResponse{}
err := json.Unmarshal([]byte(msg), &result)
return result, err
}