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;
}
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.");
}

View File

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