From 37493244b1ba0f573187b0ffbcccb02dd781568f Mon Sep 17 00:00:00 2001 From: Tom Date: Tue, 22 Oct 2024 07:47:17 +0000 Subject: [PATCH] Fixed reconnection issues. Force socket client to be a base class. --- Abstract/SocketClient.cs | 11 +++++++---- Common/WebSocketClient.cs | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Abstract/SocketClient.cs b/Abstract/SocketClient.cs index d81741b..2f082eb 100644 --- a/Abstract/SocketClient.cs +++ b/Abstract/SocketClient.cs @@ -27,6 +27,9 @@ namespace CommonSocketLibrary.Abstract _disposed = false; } + + public abstract Task Connect(); + protected async Task ConnectAsync(string url) { if (_socket != null) @@ -50,7 +53,7 @@ namespace CommonSocketLibrary.Abstract { if (_disposed || _socket == null || _cts == null) return; - + if (_socket.State == WebSocketState.Open) { _cts.CancelAfter(TimeSpan.FromMilliseconds(500)); @@ -114,7 +117,7 @@ namespace CommonSocketLibrary.Abstract } } - protected async Task Reconnect(IBackoff backoff, Action reconnect) + protected async Task Reconnect(IBackoff backoff) { while (true) { @@ -122,11 +125,11 @@ namespace CommonSocketLibrary.Abstract { TimeSpan delay = backoff.GetNextDelay(); await Task.Delay(delay); - reconnect.Invoke(); + await Connect(); backoff.Reset(); break; } - catch (Exception) + catch { _logger.Error("Unable to reconnect to server."); } diff --git a/Common/WebSocketClient.cs b/Common/WebSocketClient.cs index 2fe6ddf..f1f2e6a 100644 --- a/Common/WebSocketClient.cs +++ b/Common/WebSocketClient.cs @@ -6,7 +6,7 @@ using Serilog; namespace CommonSocketLibrary.Common { - public class WebSocketClient : SocketClient + public abstract class WebSocketClient : SocketClient { protected IDictionary _handlers; private readonly MessageTypeManager _messageTypeManager;