Skip to content

Commit

Permalink
fix(v5): wss need two messages to ping
Browse files Browse the repository at this point in the history
  • Loading branch information
hirokisan committed May 8, 2023
1 parent 80f8da7 commit 931bcfe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 6 additions & 1 deletion v5_ws_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion v5_ws_public.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 931bcfe

Please sign in to comment.