From 931bcfeb98ba93175e01eac38b8414f9abb85991 Mon Sep 17 00:00:00 2001 From: hirokisan Date: Mon, 8 May 2023 21:26:30 +0900 Subject: [PATCH] fix(v5): wss need two messages to ping --- v5_ws_private.go | 7 ++++++- v5_ws_public.go | 8 +++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/v5_ws_private.go b/v5_ws_private.go index 70862dd..3fbd5b8 100644 --- a/v5_ws_private.go +++ b/v5_ws_private.go @@ -229,7 +229,12 @@ func (s *V5WebsocketPrivateService) Run() error { // Ping : func (s *V5WebsocketPrivateService) Ping() error { - if err := s.writeMessage(websocket.PingMessage, []byte(`{"op":"ping"}`)); err != nil { + // NOTE: It appears that two messages need to be sent. + // REF: https://github.com/hirokisan/bybit/pull/127#issuecomment-1537479346 + if err := s.writeMessage(websocket.PingMessage, nil); err != nil { + return err + } + if err := s.writeMessage(websocket.TextMessage, []byte(`{"op":"ping"}`)); err != nil { return err } return nil diff --git a/v5_ws_public.go b/v5_ws_public.go index 89a1780..5bfe59d 100644 --- a/v5_ws_public.go +++ b/v5_ws_public.go @@ -130,6 +130,7 @@ func (s *V5WebsocketPublicService) Start(ctx context.Context, errHandler ErrHand go func() { defer close(done) defer s.connection.Close() + _ = s.connection.SetReadDeadline(time.Now().Add(60 * time.Second)) s.connection.SetPongHandler(func(string) error { _ = s.connection.SetReadDeadline(time.Now().Add(60 * time.Second)) @@ -234,7 +235,12 @@ func (s *V5WebsocketPublicService) Run() error { // Ping : func (s *V5WebsocketPublicService) Ping() error { - if err := s.writeMessage(websocket.PingMessage, []byte(`{"op":"ping"}`)); err != nil { + // NOTE: It appears that two messages need to be sent. + // REF: https://github.com/hirokisan/bybit/pull/127#issuecomment-1537479346 + if err := s.writeMessage(websocket.PingMessage, nil); err != nil { + return err + } + if err := s.writeMessage(websocket.TextMessage, []byte(`{"op":"ping"}`)); err != nil { return err } return nil