Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
f0cii committed Apr 22, 2020
1 parent 8bb1983 commit 08575cd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
30 changes: 20 additions & 10 deletions ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func New(config *Configuration) *ByBitWS {
}
b.ctx, b.cancel = context.WithCancel(context.Background())
b.conn = &recws.RecConn{
KeepAliveTimeout: 10 * time.Second,
KeepAliveTimeout: 60 * time.Second,
}
b.conn.SubscribeHandler = b.subscribeHandler
return b
Expand Down Expand Up @@ -155,9 +155,11 @@ func (b *ByBitWS) Start() error {
cancel := make(chan struct{})

go func() {
t := time.NewTicker(time.Second * 5)
defer t.Stop()
for {
select {
case <-time.After(30 * time.Second):
case <-t.C:
b.ping()
case <-cancel:
return
Expand Down Expand Up @@ -232,14 +234,9 @@ func (b *ByBitWS) processMessage(messageType int, data []byte) {
}

// 处理心跳包
//retMsg := ret.Get("ret_msg").String()
//if retMsg != "" && retMsg == "pong" {
// return
//}

//if ret.Get("success").Exists() {
// return
//}
if ret.Get("ret_msg").String() == "pong" {
b.handlePong()
}

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

func (b *ByBitWS) handlePong() (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprintf("handlePong error: %v", r))
}
}()
pongHandler := b.conn.PongHandler()
if pongHandler != nil {
pongHandler("pong")
}
return nil
}

func (b *ByBitWS) Close() {
b.conn.Close()
}
15 changes: 9 additions & 6 deletions ws/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import (

func TestConnect(t *testing.T) {
cfg := &Configuration{
Addr: HostTestnet,
ApiKey: "wKuYtkeNdC2PaMKjoy",
SecretKey: "5ekcDn3KnKoCRbfvrPImYzVdx7Ri2hhVxkmw",
Addr: HostTestnet,
//ApiKey: "wKuYtkeNdC2PaMKjoy",
//SecretKey: "5ekcDn3KnKoCRbfvrPImYzVdx7Ri2hhVxkmw",
ApiKey: "6IASD6KDBdunn5qLpT",
SecretKey: "nXjZMUiB3aMiPaQ9EUKYFloYNd0zM39RjRWF",
AutoReconnect: true,
DebugMode: true,
}
b := New(cfg)

Expand Down Expand Up @@ -56,15 +59,15 @@ func handleOrder(data []*Order) {
func TestOrderBookL2(t *testing.T) {
cfg := &Configuration{
Addr: HostTestnet,
ApiKey: "wKuYtkeNdC2PaMKjoy",
SecretKey: "5ekcDn3KnKoCRbfvrPImYzVdx7Ri2hhVxkmw",
ApiKey: "6IASD6KDBdunn5qLpT",
SecretKey: "nXjZMUiB3aMiPaQ9EUKYFloYNd0zM39RjRWF",
AutoReconnect: true,
DebugMode: true,
}
b := New(cfg)

// 订阅新版25档orderBook
//b.Subscribe(WSOrderBook25L1 + ".BTCUSD")
b.Subscribe(WSOrderBook25L1 + ".BTCUSD")

b.On(WSOrderBook25L1, handleOrderBook)

Expand Down

0 comments on commit 08575cd

Please sign in to comment.