Skip to content

Commit 08575cd

Browse files
committed
Update
1 parent 8bb1983 commit 08575cd

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

ws/ws.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func New(config *Configuration) *ByBitWS {
8181
}
8282
b.ctx, b.cancel = context.WithCancel(context.Background())
8383
b.conn = &recws.RecConn{
84-
KeepAliveTimeout: 10 * time.Second,
84+
KeepAliveTimeout: 60 * time.Second,
8585
}
8686
b.conn.SubscribeHandler = b.subscribeHandler
8787
return b
@@ -155,9 +155,11 @@ func (b *ByBitWS) Start() error {
155155
cancel := make(chan struct{})
156156

157157
go func() {
158+
t := time.NewTicker(time.Second * 5)
159+
defer t.Stop()
158160
for {
159161
select {
160-
case <-time.After(30 * time.Second):
162+
case <-t.C:
161163
b.ping()
162164
case <-cancel:
163165
return
@@ -232,14 +234,9 @@ func (b *ByBitWS) processMessage(messageType int, data []byte) {
232234
}
233235

234236
// 处理心跳包
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+
}
243240

244241
if topicValue := ret.Get("topic"); topicValue.Exists() {
245242
topic := topicValue.String()
@@ -352,6 +349,19 @@ func (b *ByBitWS) processMessage(messageType int, data []byte) {
352349
}
353350
}
354351

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+
355365
func (b *ByBitWS) Close() {
356366
b.conn.Close()
357367
}

ws/ws_test.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import (
88

99
func TestConnect(t *testing.T) {
1010
cfg := &Configuration{
11-
Addr: HostTestnet,
12-
ApiKey: "wKuYtkeNdC2PaMKjoy",
13-
SecretKey: "5ekcDn3KnKoCRbfvrPImYzVdx7Ri2hhVxkmw",
11+
Addr: HostTestnet,
12+
//ApiKey: "wKuYtkeNdC2PaMKjoy",
13+
//SecretKey: "5ekcDn3KnKoCRbfvrPImYzVdx7Ri2hhVxkmw",
14+
ApiKey: "6IASD6KDBdunn5qLpT",
15+
SecretKey: "nXjZMUiB3aMiPaQ9EUKYFloYNd0zM39RjRWF",
1416
AutoReconnect: true,
17+
DebugMode: true,
1518
}
1619
b := New(cfg)
1720

@@ -56,15 +59,15 @@ func handleOrder(data []*Order) {
5659
func TestOrderBookL2(t *testing.T) {
5760
cfg := &Configuration{
5861
Addr: HostTestnet,
59-
ApiKey: "wKuYtkeNdC2PaMKjoy",
60-
SecretKey: "5ekcDn3KnKoCRbfvrPImYzVdx7Ri2hhVxkmw",
62+
ApiKey: "6IASD6KDBdunn5qLpT",
63+
SecretKey: "nXjZMUiB3aMiPaQ9EUKYFloYNd0zM39RjRWF",
6164
AutoReconnect: true,
6265
DebugMode: true,
6366
}
6467
b := New(cfg)
6568

6669
// 订阅新版25档orderBook
67-
//b.Subscribe(WSOrderBook25L1 + ".BTCUSD")
70+
b.Subscribe(WSOrderBook25L1 + ".BTCUSD")
6871

6972
b.On(WSOrderBook25L1, handleOrderBook)
7073

0 commit comments

Comments
 (0)