Skip to content

Commit

Permalink
echo ping data for binance
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Sep 21, 2024
1 parent ae2fbd0 commit 884c28e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 12 additions & 2 deletions client/comms/wsconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ type WsCfg struct {
DisableAutoReconnect bool

ConnectHeaders http.Header

// EchoPingData will echo any data from pings as the pong data.
EchoPingData bool
}

// wsConn represents a client websocket connection.
Expand Down Expand Up @@ -258,7 +261,9 @@ func (conn *wsConn) connect(ctx context.Context) error {
return err
}

ws.SetPingHandler(func(string) error {
echoPing := conn.cfg.EchoPingData

ws.SetPingHandler(func(appData string) error {
now := time.Now()

// Set the deadline for the next ping.
Expand All @@ -268,8 +273,13 @@ func (conn *wsConn) connect(ctx context.Context) error {
return err
}

var data []byte
if echoPing {
data = []byte(appData)
}

// Respond with a pong.
err = ws.WriteControl(websocket.PongMessage, []byte{}, now.Add(writeWait))
err = ws.WriteControl(websocket.PongMessage, data, now.Add(writeWait))
if err != nil {
// read loop handles reconnect
conn.log.Errorf("pong write error: %v", err)
Expand Down
8 changes: 5 additions & 3 deletions client/mm/libxc/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1390,8 +1390,9 @@ func (bnc *binance) getUserDataStream(ctx context.Context) (err error) {
}

conn, err := comms.NewWsConn(&comms.WsCfg{
URL: bnc.wsURL + "/ws/" + listenKey,
PingWait: time.Minute * 4,
URL: bnc.wsURL + "/ws/" + listenKey,
PingWait: time.Minute * 4,
EchoPingData: true,
ReconnectSync: func() {
bnc.log.Debugf("Binance reconnected")
},
Expand Down Expand Up @@ -1601,7 +1602,8 @@ func (bnc *binance) connectToMarketDataStream(ctx context.Context, baseID, quote
// minutes. If the websocket server does not receive a pong frame
// back from the connection within a 10 minute period, the connection
// will be disconnected. Unsolicited pong frames are allowed.
PingWait: time.Minute * 4,
PingWait: time.Minute * 4,
EchoPingData: true,
ReconnectSync: func() {
bnc.log.Debugf("Binance reconnected")
},
Expand Down

0 comments on commit 884c28e

Please sign in to comment.