package marketwssclient import ( "fmt" "wss-pool/logging/applogger" "wss-pool/pkg/client/hbwebsocketclientbase" ) // Responsible to handle candlestick data from WebSocket type ContractDepthWebSocketClient struct { hbwebsocketclientbase.WebSocketClientBase } // Initializer func (p *ContractDepthWebSocketClient) Init(host string) *ContractDepthWebSocketClient { p.WebSocketClientBase.Init(host) return p } // Set callback handler func (p *ContractDepthWebSocketClient) 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 *ContractDepthWebSocketClient) Request(symbol string, period string, from int64, to int64, clientId string) { topic := fmt.Sprintf("market.%s.depth.%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) } // Subscribe candlestick data func (p *ContractDepthWebSocketClient) Subscribe(symbol, period string, clientId string) { topic := fmt.Sprintf("market.%s.depth.%s", symbol, period) sub := fmt.Sprintf("{\"sub\": \"%s\", \"id\": \"%s\"}", topic, clientId) fmt.Println("Subscribe :", p.Symbol) p.Symbol = topic p.Send(sub) applogger.Info("WebSocket subscribed, topic=%s, clientId=%s", topic, clientId) } // Unsubscribe candlestick data func (p *ContractDepthWebSocketClient) UnSubscribe(symbol string, period string, clientId string) { topic := fmt.Sprintf("market.%s.depth.%s", symbol, period) unsub := fmt.Sprintf("{\"unsub\": \"%s\", \"id\": \"%s\" }", topic, clientId) p.Send(unsub) applogger.Info("WebSocket unsubscribed, topic=%s, clientId=%s", topic, clientId) } func (p *ContractDepthWebSocketClient) handleMessage(msg string) (interface{}, error) { // result := market.SubscribeCtDepthResponse{} //err := json.Unmarshal([]byte(msg), &result) //return msg,nil return []byte(msg), nil }