mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-11 18:06:27 +00:00
Added autoreconnect option
This commit is contained in:
parent
4536d5f6c6
commit
4cae8670a3
@ -78,6 +78,11 @@ namespace CryptoExchange.Net.Objects
|
|||||||
|
|
||||||
public class SocketClientOptions: ExchangeOptions
|
public class SocketClientOptions: ExchangeOptions
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Whether or not the socket should automatically reconnect when losing connection
|
||||||
|
/// </summary>
|
||||||
|
public bool AutoReconnect { get; set; } = true;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Time to wait between reconnect attempts
|
/// Time to wait between reconnect attempts
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -91,6 +96,7 @@ namespace CryptoExchange.Net.Objects
|
|||||||
LogVerbosity = LogVerbosity,
|
LogVerbosity = LogVerbosity,
|
||||||
Proxy = Proxy,
|
Proxy = Proxy,
|
||||||
LogWriters = LogWriters,
|
LogWriters = LogWriters,
|
||||||
|
AutoReconnect = AutoReconnect,
|
||||||
ReconnectInterval = ReconnectInterval
|
ReconnectInterval = ReconnectInterval
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@ namespace CryptoExchange.Net
|
|||||||
protected List<SocketSubscription> sockets = new List<SocketSubscription>();
|
protected List<SocketSubscription> sockets = new List<SocketSubscription>();
|
||||||
|
|
||||||
public TimeSpan ReconnectInterval { get; private set; }
|
public TimeSpan ReconnectInterval { get; private set; }
|
||||||
|
public bool AutoReconnect { get; private set; }
|
||||||
protected Func<byte[], string> dataInterpreter;
|
protected Func<byte[], string> dataInterpreter;
|
||||||
|
|
||||||
protected const string DataHandlerName = "DataHandler";
|
protected const string DataHandlerName = "DataHandler";
|
||||||
@ -48,6 +49,7 @@ namespace CryptoExchange.Net
|
|||||||
/// <param name="exchangeOptions">Options</param>
|
/// <param name="exchangeOptions">Options</param>
|
||||||
protected void Configure(SocketClientOptions exchangeOptions)
|
protected void Configure(SocketClientOptions exchangeOptions)
|
||||||
{
|
{
|
||||||
|
AutoReconnect = exchangeOptions.AutoReconnect;
|
||||||
ReconnectInterval = exchangeOptions.ReconnectInterval;
|
ReconnectInterval = exchangeOptions.ReconnectInterval;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,7 +170,7 @@ namespace CryptoExchange.Net
|
|||||||
/// <param name="socket">The socket that was closed</param>
|
/// <param name="socket">The socket that was closed</param>
|
||||||
protected virtual void SocketOnClose(IWebsocket socket)
|
protected virtual void SocketOnClose(IWebsocket socket)
|
||||||
{
|
{
|
||||||
if (socket.ShouldReconnect)
|
if (AutoReconnect && socket.ShouldReconnect)
|
||||||
{
|
{
|
||||||
if (socket.Reconnecting)
|
if (socket.Reconnecting)
|
||||||
return; // Already reconnecting
|
return; // Already reconnecting
|
||||||
@ -181,6 +183,13 @@ namespace CryptoExchange.Net
|
|||||||
while (socket.ShouldReconnect)
|
while (socket.ShouldReconnect)
|
||||||
{
|
{
|
||||||
Thread.Sleep(ReconnectInterval);
|
Thread.Sleep(ReconnectInterval);
|
||||||
|
if (!socket.ShouldReconnect)
|
||||||
|
{
|
||||||
|
// Should reconnect changed to false while waiting to reconnect
|
||||||
|
socket.Reconnecting = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
socket.Reset();
|
socket.Reset();
|
||||||
if (!socket.Connect().Result)
|
if (!socket.Connect().Result)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user