Added auto reconnect on first try
This commit is contained in:
parent
00aab13685
commit
8c6eccb53f
|
@ -15,6 +15,12 @@ namespace EonaCat.Connections.Client.Example
|
|||
|
||||
while (true)
|
||||
{
|
||||
if (!_client.IsConnected)
|
||||
{
|
||||
await Task.Delay(1000).ConfigureAwait(false);
|
||||
continue;
|
||||
}
|
||||
|
||||
Console.Write("Enter message to send (or 'exit' to quit): ");
|
||||
var message = Console.ReadLine();
|
||||
|
||||
|
@ -46,10 +52,19 @@ namespace EonaCat.Connections.Client.Example
|
|||
|
||||
_client = new NetworkClient(config);
|
||||
|
||||
_client.OnGeneralError += (sender, e) =>
|
||||
Console.WriteLine($"Error: {e.Message}");
|
||||
|
||||
// Subscribe to events
|
||||
_client.OnConnected += (sender, e) =>
|
||||
_client.OnConnected += async (sender, e) =>
|
||||
{
|
||||
Console.WriteLine($"Connected to server at {e.RemoteEndPoint}");
|
||||
|
||||
// Send nickname
|
||||
await _client.SendNicknameAsync("TestUser");
|
||||
|
||||
// Send a message
|
||||
await _client.SendAsync("Hello server!");
|
||||
};
|
||||
|
||||
_client.OnDataReceived += (sender, e) =>
|
||||
|
@ -60,13 +75,8 @@ namespace EonaCat.Connections.Client.Example
|
|||
Console.WriteLine("Disconnected from server");
|
||||
};
|
||||
|
||||
Console.WriteLine("Connecting to server...");
|
||||
await _client.ConnectAsync();
|
||||
|
||||
// Send nickname
|
||||
await _client.SendNicknameAsync("TestUser");
|
||||
|
||||
// Send a message
|
||||
await _client.SendAsync("Hello server!");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -24,6 +24,8 @@ namespace EonaCat.Connections
|
|||
private CancellationTokenSource _cancellation;
|
||||
private bool _isConnected;
|
||||
|
||||
public bool IsConnected => _isConnected;
|
||||
|
||||
public event EventHandler<ConnectionEventArgs> OnConnected;
|
||||
public event EventHandler<DataReceivedEventArgs> OnDataReceived;
|
||||
public event EventHandler<ConnectionEventArgs> OnDisconnected;
|
||||
|
@ -106,12 +108,17 @@ namespace EonaCat.Connections
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_isConnected = false;
|
||||
OnGeneralError?.Invoke(this, new ErrorEventArgs { Exception = ex, Message = "Failed to connect" });
|
||||
await Task.Run(() => AutoReconnectAsync());
|
||||
}
|
||||
}
|
||||
|
||||
public string IpAddress => _config != null ? _config.Host : string.Empty;
|
||||
public int Port => _config != null ? _config.Port : 0;
|
||||
|
||||
public bool IsAutoReconnecting { get; private set; }
|
||||
|
||||
private async Task ConnectUdp()
|
||||
{
|
||||
await Task.Run(() =>
|
||||
|
@ -129,6 +136,7 @@ namespace EonaCat.Connections
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_isConnected = false;
|
||||
OnGeneralError?.Invoke(this, new ErrorEventArgs { Exception = ex, Message = "Failed to connect UDP" });
|
||||
}
|
||||
});
|
||||
|
@ -186,8 +194,8 @@ namespace EonaCat.Connections
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
OnGeneralError?.Invoke(this, new ErrorEventArgs { Exception = ex, Message = "Error receiving data" });
|
||||
_isConnected = false;
|
||||
OnGeneralError?.Invoke(this, new ErrorEventArgs { Exception = ex, Message = "Error receiving data" });
|
||||
|
||||
_ = Task.Run(() => AutoReconnectAsync());
|
||||
break;
|
||||
|
@ -377,6 +385,11 @@ namespace EonaCat.Connections
|
|||
return;
|
||||
}
|
||||
|
||||
if (IsAutoReconnecting)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int attempt = 0;
|
||||
|
||||
while (!_isConnected && (_config.MaxReconnectAttempts == 0 || attempt < _config.MaxReconnectAttempts))
|
||||
|
@ -385,10 +398,12 @@ namespace EonaCat.Connections
|
|||
try
|
||||
{
|
||||
OnGeneralError?.Invoke(this, new ErrorEventArgs { Message = $"Attempting to reconnect (Attempt {attempt})" });
|
||||
IsAutoReconnecting = true;
|
||||
await ConnectAsync();
|
||||
|
||||
if (_isConnected)
|
||||
{
|
||||
IsAutoReconnecting = false;
|
||||
OnGeneralError?.Invoke(this, new ErrorEventArgs { Message = $"Reconnected successfully after {attempt} attempt(s)" });
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue