@@ -81,7 +81,7 @@ func New(config *Configuration) *ByBitWS {
81
81
}
82
82
b .ctx , b .cancel = context .WithCancel (context .Background ())
83
83
b .conn = & recws.RecConn {
84
- KeepAliveTimeout : 10 * time .Second ,
84
+ KeepAliveTimeout : 60 * time .Second ,
85
85
}
86
86
b .conn .SubscribeHandler = b .subscribeHandler
87
87
return b
@@ -155,9 +155,11 @@ func (b *ByBitWS) Start() error {
155
155
cancel := make (chan struct {})
156
156
157
157
go func () {
158
+ t := time .NewTicker (time .Second * 5 )
159
+ defer t .Stop ()
158
160
for {
159
161
select {
160
- case <- time . After ( 30 * time . Second ) :
162
+ case <- t . C :
161
163
b .ping ()
162
164
case <- cancel :
163
165
return
@@ -232,14 +234,9 @@ func (b *ByBitWS) processMessage(messageType int, data []byte) {
232
234
}
233
235
234
236
// 处理心跳包
235
- //retMsg := ret.Get("ret_msg").String()
236
- //if retMsg != "" && retMsg == "pong" {
237
- // return
238
- //}
239
-
240
- //if ret.Get("success").Exists() {
241
- // return
242
- //}
237
+ if ret .Get ("ret_msg" ).String () == "pong" {
238
+ b .handlePong ()
239
+ }
243
240
244
241
if topicValue := ret .Get ("topic" ); topicValue .Exists () {
245
242
topic := topicValue .String ()
@@ -352,6 +349,19 @@ func (b *ByBitWS) processMessage(messageType int, data []byte) {
352
349
}
353
350
}
354
351
352
+ func (b * ByBitWS ) handlePong () (err error ) {
353
+ defer func () {
354
+ if r := recover (); r != nil {
355
+ err = errors .New (fmt .Sprintf ("handlePong error: %v" , r ))
356
+ }
357
+ }()
358
+ pongHandler := b .conn .PongHandler ()
359
+ if pongHandler != nil {
360
+ pongHandler ("pong" )
361
+ }
362
+ return nil
363
+ }
364
+
355
365
func (b * ByBitWS ) Close () {
356
366
b .conn .Close ()
357
367
}
0 commit comments