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
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 Depth data from WebSocket
|
|
type DepthWebSocketClient struct {
|
|
hbwebsocketclientbase.WebSocketClientBase
|
|
}
|
|
|
|
// Initializer
|
|
func (p *DepthWebSocketClient) Init(host string) *DepthWebSocketClient {
|
|
p.WebSocketClientBase.Init(host)
|
|
return p
|
|
}
|
|
|
|
// Set callback handler
|
|
func (p *DepthWebSocketClient) SetHandler(
|
|
connectedHandler hbwebsocketclientbase.ConnectedHandler,
|
|
responseHandler hbwebsocketclientbase.ResponseHandler) {
|
|
p.WebSocketClientBase.SetHandler(connectedHandler, p.handleMessage, responseHandler)
|
|
}
|
|
|
|
// Request full depth data
|
|
func (p *DepthWebSocketClient) Request(symbol string, step string, clientId string) {
|
|
topic := fmt.Sprintf("market.%s.depth.%s", symbol, step)
|
|
req := fmt.Sprintf("{\"req\": \"%s\",\"id\": \"%s\" }", topic, clientId)
|
|
|
|
p.WebSocketClientBase.Send(req)
|
|
|
|
applogger.Info("WebSocket requested, topic=%s, clientId=%s", topic, clientId)
|
|
}
|
|
|
|
// Subscribe latest market by price order book in snapshot mode at 1-second interval.
|
|
func (p *DepthWebSocketClient) Subscribe(symbol string, step string, clientId string) {
|
|
topic := fmt.Sprintf("market.%s.depth.%s", symbol, step)
|
|
sub := fmt.Sprintf("{\"sub\": \"%s\",\"id\": \"%s\" }", topic, clientId)
|
|
|
|
p.Send(sub)
|
|
|
|
applogger.Info("WebSocket subscribed, topic=%s, clientId=%s", topic, clientId)
|
|
}
|
|
|
|
// Unsubscribe market by price order book
|
|
func (p *DepthWebSocketClient) UnSubscribe(symbol string, step string, clientId string) {
|
|
topic := fmt.Sprintf("market.%s.depth.%s", symbol, step)
|
|
unsub := fmt.Sprintf("{\"unsub\": \"%s\",\"id\": \"%s\" }", topic, clientId)
|
|
|
|
p.Send(unsub)
|
|
|
|
applogger.Info("WebSocket unsubscribed, topic=%s, clientId=%s", topic, clientId)
|
|
}
|
|
|
|
func (p *DepthWebSocketClient) handleMessage(msg string) (interface{}, error) {
|
|
result := market.SubscribeDepthResponse{}
|
|
err := json.Unmarshal([]byte(msg), &result)
|
|
return result, err
|
|
}
|
|
|