Skip to content

Commit

Permalink
Cleanup timer when there is a WS error
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Molinero committed Nov 16, 2023
1 parent dbfb055 commit a720409
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions QuantConnect.BybitBrokerage/BybitWebSocketWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class BybitWebSocketWrapper : WebSocketClientWrapper
/// </summary>
protected override void OnOpen()
{
CleanUpTimer();
_pingTimer = new Timer(TimeSpan.FromSeconds(20).TotalMilliseconds);
_pingTimer.Elapsed += PingTimerElapsed;
_pingTimer.Start();
Expand All @@ -41,6 +42,24 @@ protected override void OnOpen()
/// Event invocator for the <see cref="WebSocketClientWrapper.Close"/> event
/// </summary>
protected override void OnClose(WebSocketCloseData e)
{
CleanUpTimer();
base.OnClose(e);
}

/// <summary>
/// Event invocator for the <see cref="WebSocketClientWrapper.OnError"/> event
/// </summary>
protected override void OnError(WebSocketError e)
{
CleanUpTimer();
base.OnError(e);
}

/// <summary>
/// Helper method to clean up timer if required
/// </summary>
private void CleanUpTimer()
{
if (_pingTimer != null)
{
Expand All @@ -49,11 +68,8 @@ protected override void OnClose(WebSocketCloseData e)
_pingTimer.Dispose();
_pingTimer = null;
}

base.OnClose(e);
}



private void PingTimerElapsed(object sender, ElapsedEventArgs e)
{
Send("{\"op\":\"ping\"}");
Expand Down

0 comments on commit a720409

Please sign in to comment.