Fixed reconnection issues. Force socket client to be a base class.

This commit is contained in:
Tom 2024-10-22 07:47:17 +00:00
parent ab90d47b89
commit 37493244b1
2 changed files with 8 additions and 5 deletions

View File

@ -27,6 +27,9 @@ namespace CommonSocketLibrary.Abstract
_disposed = false; _disposed = false;
} }
public abstract Task Connect();
protected async Task ConnectAsync(string url) protected async Task ConnectAsync(string url)
{ {
if (_socket != null) if (_socket != null)
@ -50,7 +53,7 @@ namespace CommonSocketLibrary.Abstract
{ {
if (_disposed || _socket == null || _cts == null) if (_disposed || _socket == null || _cts == null)
return; return;
if (_socket.State == WebSocketState.Open) if (_socket.State == WebSocketState.Open)
{ {
_cts.CancelAfter(TimeSpan.FromMilliseconds(500)); _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) while (true)
{ {
@ -122,11 +125,11 @@ namespace CommonSocketLibrary.Abstract
{ {
TimeSpan delay = backoff.GetNextDelay(); TimeSpan delay = backoff.GetNextDelay();
await Task.Delay(delay); await Task.Delay(delay);
reconnect.Invoke(); await Connect();
backoff.Reset(); backoff.Reset();
break; break;
} }
catch (Exception) catch
{ {
_logger.Error("Unable to reconnect to server."); _logger.Error("Unable to reconnect to server.");
} }

View File

@ -6,7 +6,7 @@ using Serilog;
namespace CommonSocketLibrary.Common namespace CommonSocketLibrary.Common
{ {
public class WebSocketClient : SocketClient<WebSocketMessage> public abstract class WebSocketClient : SocketClient<WebSocketMessage>
{ {
protected IDictionary<int, IWebSocketHandler> _handlers; protected IDictionary<int, IWebSocketHandler> _handlers;
private readonly MessageTypeManager<IWebSocketHandler> _messageTypeManager; private readonly MessageTypeManager<IWebSocketHandler> _messageTypeManager;