1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-10 09:26:22 +00:00

Added autoreconnect option

This commit is contained in:
Jan Korf 2019-03-18 10:41:03 +01:00
parent 4536d5f6c6
commit 4cae8670a3
2 changed files with 16 additions and 1 deletions

View File

@ -78,6 +78,11 @@ namespace CryptoExchange.Net.Objects
public class SocketClientOptions: ExchangeOptions
{
/// <summary>
/// Whether or not the socket should automatically reconnect when losing connection
/// </summary>
public bool AutoReconnect { get; set; } = true;
/// <summary>
/// Time to wait between reconnect attempts
/// </summary>
@ -91,6 +96,7 @@ namespace CryptoExchange.Net.Objects
LogVerbosity = LogVerbosity,
Proxy = Proxy,
LogWriters = LogWriters,
AutoReconnect = AutoReconnect,
ReconnectInterval = ReconnectInterval
};

View File

@ -25,6 +25,7 @@ namespace CryptoExchange.Net
protected List<SocketSubscription> sockets = new List<SocketSubscription>();
public TimeSpan ReconnectInterval { get; private set; }
public bool AutoReconnect { get; private set; }
protected Func<byte[], string> dataInterpreter;
protected const string DataHandlerName = "DataHandler";
@ -48,6 +49,7 @@ namespace CryptoExchange.Net
/// <param name="exchangeOptions">Options</param>
protected void Configure(SocketClientOptions exchangeOptions)
{
AutoReconnect = exchangeOptions.AutoReconnect;
ReconnectInterval = exchangeOptions.ReconnectInterval;
}
@ -168,7 +170,7 @@ namespace CryptoExchange.Net
/// <param name="socket">The socket that was closed</param>
protected virtual void SocketOnClose(IWebsocket socket)
{
if (socket.ShouldReconnect)
if (AutoReconnect && socket.ShouldReconnect)
{
if (socket.Reconnecting)
return; // Already reconnecting
@ -181,6 +183,13 @@ namespace CryptoExchange.Net
while (socket.ShouldReconnect)
{
Thread.Sleep(ReconnectInterval);
if (!socket.ShouldReconnect)
{
// Should reconnect changed to false while waiting to reconnect
socket.Reconnecting = false;
return;
}
socket.Reset();
if (!socket.Connect().Result)
{