package marketwssclient import ( "encoding/json" "fmt" "wss-pool/logging/applogger" "wss-pool/pkg/client/hbwebsocketclientbase" "wss-pool/pkg/model/market" ) // Responsible to handle Trade data from WebSocket type TickerWebSocketClient struct { hbwebsocketclientbase.WebSocketClientBase } // Initializer func (p *TickerWebSocketClient) Init(host string) *TickerWebSocketClient { p.WebSocketClientBase.Init(host) return p } // Set callback handler func (p *TickerWebSocketClient) SetHandler( connectedHandler hbwebsocketclientbase.ConnectedHandler, responseHandler hbwebsocketclientbase.ResponseHandler) { p.WebSocketClientBase.SetHandler(connectedHandler, p.handleMessage, responseHandler) } // Request latest 300 trade data func (p *TickerWebSocketClient) Request(symbol string, clientId string) { topic := fmt.Sprintf("market.%s.ticker", symbol) req := fmt.Sprintf("{\"req\": \"%s\",\"id\": \"%s\" }", topic, clientId) p.Send(req) applogger.Info("WebSocket requested, topic=%s, clientId=%s", topic, clientId) } // Subscribe latest completed trade in tick by tick mode func (p *TickerWebSocketClient) Subscribe(symbol string, clientId string) { topic := fmt.Sprintf("market.%s.ticker", symbol) sub := fmt.Sprintf("{\"sub\": \"%s\",\"id\": \"%s\" }", topic, clientId) p.Send(sub) applogger.Info("WebSocket subscribed, topic=%s, clientId=%s", topic, clientId) } // Unsubscribe trade func (p *TickerWebSocketClient) UnSubscribe(symbol string, clientId string) { topic := fmt.Sprintf("market.%s.ticker", 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 *TickerWebSocketClient) handleMessage(msg string) (interface{}, error) { result := market.TickerWebsocketResponse{} err := json.Unmarshal([]byte(msg), &result) return result, err }