Merge pull request #1267 from bailantaotao/edwin/add-heart-beat-fn

FEATURE: add custom heart beat func to StandardStream
This commit is contained in:
bailantaotao 2023-08-02 17:54:50 +08:00 committed by GitHub
commit c1d7bdfb65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -41,6 +41,9 @@ type Parser func(message []byte) (interface{}, error)
type Dispatcher func(e interface{})
// HeartBeat keeps connection alive by sending the heartbeat packet.
type HeartBeat func(ctxConn context.Context, conn *websocket.Conn, cancelConn context.CancelFunc)
//go:generate callbackgen -type StandardStream -interface
type StandardStream struct {
parser Parser
@ -106,6 +109,8 @@ type StandardStream struct {
FuturesPositionUpdateCallbacks []func(futuresPositions FuturesPositionMap)
FuturesPositionSnapshotCallbacks []func(futuresPositions FuturesPositionMap)
heartBeat HeartBeat
}
type StandardStreamEmitter interface {
@ -350,6 +355,9 @@ func (s *StandardStream) DialAndConnect(ctx context.Context) error {
go s.Read(connCtx, conn, connCancel)
go s.ping(connCtx, conn, connCancel, pingInterval)
if s.heartBeat != nil {
go s.heartBeat(connCtx, conn, connCancel)
}
return nil
}
@ -419,6 +427,11 @@ func (s *StandardStream) Close() error {
return nil
}
// SetHeartBeat sets the custom heart beat implementation if needed
func (s *StandardStream) SetHeartBeat(fn HeartBeat) {
s.heartBeat = fn
}
type Depth string
const (