Skip to content

Commit

Permalink
Huobi: Clarify V1/V2 pings
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Dec 14, 2024
1 parent 32d533c commit a89f6f8
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions exchanges/huobi/huobi_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,14 @@ func (h *HUOBI) wsHandleData(respRaw []byte) error {
}
}

if ping, err := jsonparser.GetInt(respRaw, "ping"); err == nil {
if err := h.Websocket.Conn.SendJSONMessage(context.Background(), request.Unset, json.RawMessage(`{"pong":`+strconv.Itoa(int(ping))+`}`)); err != nil {
return fmt.Errorf("error sending pong response: %w", err)
}
return nil
if pingValue, err := jsonparser.GetInt(respRaw, "ping"); err == nil {
return h.wsHandleV1ping(int(pingValue))
}

if action, err := jsonparser.GetString(respRaw, "action"); err == nil {
switch action {
case "ping":
return h.wsHandleV2ping(action, respRaw)
return h.wsHandleV2ping(respRaw)
case wsSubOp, wsUnsubOp:
return h.wsHandleV2subResp(action, respRaw)
}
Expand All @@ -169,7 +166,16 @@ func (h *HUOBI) wsHandleData(respRaw []byte) error {
return nil
}

func (h *HUOBI) wsHandleV2ping(_ string, respRaw []byte) error {
// wsHandleV1ping handles v1 style pings, currently only used with public connections
func (h *HUOBI) wsHandleV1ping(pingValue int) error {
if err := h.Websocket.Conn.SendJSONMessage(context.Background(), request.Unset, json.RawMessage(`{"pong":`+strconv.Itoa(pingValue)+`}`)); err != nil {
return fmt.Errorf("error sending pong response: %w", err)
}
return nil
}

// wsHandleV2ping handles v2 style pings, currently only used with private connections
func (h *HUOBI) wsHandleV2ping(respRaw []byte) error {
ts, err := jsonparser.GetInt(respRaw, "data", "ts")
if err != nil {
return fmt.Errorf("error getting ts from auth ping: %w", err)
Expand Down

0 comments on commit a89f6f8

Please sign in to comment.