mirror of
https://github.com/JKorf/CryptoExchange.Net.git
synced 2026-08-12 08:53:01 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 99c331b389 | |||
| 70f8bd203a | |||
| 89b517c936 | |||
| 91e33cc42c |
@@ -42,7 +42,7 @@ namespace CryptoExchange.Net.UnitTests
|
||||
socket.CanConnect = canConnect;
|
||||
|
||||
//act
|
||||
var connectResult = client.ConnectSocketSub(new SocketConnection(client, null, socket, null));
|
||||
var connectResult = client.ConnectSocketSub(new SocketConnection(client, null, socket));
|
||||
|
||||
//assert
|
||||
Assert.IsTrue(connectResult.Success == canConnect);
|
||||
@@ -57,10 +57,10 @@ namespace CryptoExchange.Net.UnitTests
|
||||
socket.ShouldReconnect = true;
|
||||
socket.CanConnect = true;
|
||||
socket.DisconnectTime = DateTime.UtcNow;
|
||||
var sub = new SocketConnection(client, null, socket, null);
|
||||
var sub = new SocketConnection(client, null, socket);
|
||||
var rstEvent = new ManualResetEvent(false);
|
||||
JToken result = null;
|
||||
sub.AddSubscription(SocketSubscription.CreateForIdentifier(10, "TestHandler", true, false, (messageEvent) =>
|
||||
sub.AddSubscription(SocketSubscription.CreateForIdentifier(10, "TestHandler", true, (messageEvent) =>
|
||||
{
|
||||
result = messageEvent.JsonData;
|
||||
rstEvent.Set();
|
||||
@@ -85,10 +85,10 @@ namespace CryptoExchange.Net.UnitTests
|
||||
socket.ShouldReconnect = true;
|
||||
socket.CanConnect = true;
|
||||
socket.DisconnectTime = DateTime.UtcNow;
|
||||
var sub = new SocketConnection(client, null, socket, null);
|
||||
var sub = new SocketConnection(client, null, socket);
|
||||
var rstEvent = new ManualResetEvent(false);
|
||||
string original = null;
|
||||
sub.AddSubscription(SocketSubscription.CreateForIdentifier(10, "TestHandler", true, false, (messageEvent) =>
|
||||
sub.AddSubscription(SocketSubscription.CreateForIdentifier(10, "TestHandler", true, (messageEvent) =>
|
||||
{
|
||||
original = messageEvent.OriginalData;
|
||||
rstEvent.Set();
|
||||
@@ -103,6 +103,34 @@ namespace CryptoExchange.Net.UnitTests
|
||||
Assert.IsTrue(original == (enabled ? "{\"property\": 123}" : null));
|
||||
}
|
||||
|
||||
[TestCase]
|
||||
public void DisconnectedSocket_Should_Reconnect()
|
||||
{
|
||||
// arrange
|
||||
bool reconnected = false;
|
||||
var client = new TestSocketClient(new TestOptions() { ReconnectInterval = TimeSpan.Zero, LogLevel = LogLevel.Debug });
|
||||
var socket = client.CreateSocket();
|
||||
socket.ShouldReconnect = true;
|
||||
socket.CanConnect = true;
|
||||
socket.DisconnectTime = DateTime.UtcNow;
|
||||
var sub = new SocketConnection(client, null, socket);
|
||||
sub.ShouldReconnect = true;
|
||||
client.ConnectSocketSub(sub);
|
||||
var rstEvent = new ManualResetEvent(false);
|
||||
sub.ConnectionRestored += (a) =>
|
||||
{
|
||||
reconnected = true;
|
||||
rstEvent.Set();
|
||||
};
|
||||
|
||||
// act
|
||||
socket.InvokeClose();
|
||||
rstEvent.WaitOne(1000);
|
||||
|
||||
// assert
|
||||
Assert.IsTrue(reconnected);
|
||||
}
|
||||
|
||||
[TestCase()]
|
||||
public void UnsubscribingStream_Should_CloseTheSocket()
|
||||
{
|
||||
@@ -110,11 +138,9 @@ namespace CryptoExchange.Net.UnitTests
|
||||
var client = new TestSocketClient(new TestOptions() { ReconnectInterval = TimeSpan.Zero, LogLevel = LogLevel.Debug });
|
||||
var socket = client.CreateSocket();
|
||||
socket.CanConnect = true;
|
||||
var sub = new SocketConnection(client, null, socket, null);
|
||||
var sub = new SocketConnection(client, null, socket);
|
||||
client.ConnectSocketSub(sub);
|
||||
var us = SocketSubscription.CreateForIdentifier(10, "Test", true, false, (e) => { });
|
||||
var ups = new UpdateSubscription(sub, us);
|
||||
sub.AddSubscription(us);
|
||||
var ups = new UpdateSubscription(sub, SocketSubscription.CreateForIdentifier(10, "Test", true, (e) => {}));
|
||||
|
||||
// act
|
||||
client.UnsubscribeAsync(ups).Wait();
|
||||
@@ -132,8 +158,8 @@ namespace CryptoExchange.Net.UnitTests
|
||||
var socket2 = client.CreateSocket();
|
||||
socket1.CanConnect = true;
|
||||
socket2.CanConnect = true;
|
||||
var sub1 = new SocketConnection(client, null, socket1, null);
|
||||
var sub2 = new SocketConnection(client, null, socket2, null);
|
||||
var sub1 = new SocketConnection(client, null, socket1);
|
||||
var sub2 = new SocketConnection(client, null, socket2);
|
||||
client.ConnectSocketSub(sub1);
|
||||
client.ConnectSocketSub(sub2);
|
||||
|
||||
@@ -152,7 +178,7 @@ namespace CryptoExchange.Net.UnitTests
|
||||
var client = new TestSocketClient(new TestOptions() { ReconnectInterval = TimeSpan.Zero, LogLevel = LogLevel.Debug });
|
||||
var socket = client.CreateSocket();
|
||||
socket.CanConnect = false;
|
||||
var sub = new SocketConnection(client, null, socket, null);
|
||||
var sub = new SocketConnection(client, null, socket);
|
||||
|
||||
// act
|
||||
var connectResult = client.ConnectSocketSub(sub);
|
||||
|
||||
@@ -13,15 +13,9 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
|
||||
public bool Connected { get; set; }
|
||||
|
||||
public event Action OnClose;
|
||||
|
||||
#pragma warning disable 0067
|
||||
public event Action OnReconnected;
|
||||
public event Action OnReconnecting;
|
||||
#pragma warning restore 0067
|
||||
public event Action<string> OnMessage;
|
||||
public event Action<Exception> OnError;
|
||||
public event Action OnOpen;
|
||||
public Func<Task<Uri>> GetReconnectionUrl { get; set; }
|
||||
|
||||
public int Id { get; }
|
||||
public bool ShouldReconnect { get; set; }
|
||||
@@ -99,7 +93,6 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
|
||||
{
|
||||
Connected = false;
|
||||
DisconnectTime = DateTime.UtcNow;
|
||||
Reconnecting = true;
|
||||
OnClose?.Invoke();
|
||||
}
|
||||
|
||||
@@ -122,6 +115,11 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
|
||||
{
|
||||
OnError?.Invoke(error);
|
||||
}
|
||||
public Task ReconnectAsync() => Task.CompletedTask;
|
||||
|
||||
public async Task ProcessAsync()
|
||||
{
|
||||
while (Connected)
|
||||
await Task.Delay(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,13 +22,13 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
|
||||
{
|
||||
SubClient = new TestSubSocketClient(exchangeOptions, exchangeOptions.SubOptions);
|
||||
SocketFactory = new Mock<IWebsocketFactory>().Object;
|
||||
Mock.Get(SocketFactory).Setup(f => f.CreateWebsocket(It.IsAny<Log>(), It.IsAny<WebSocketParameters>())).Returns(new TestSocket());
|
||||
Mock.Get(SocketFactory).Setup(f => f.CreateWebsocket(It.IsAny<Log>(), It.IsAny<string>())).Returns(new TestSocket());
|
||||
}
|
||||
|
||||
public TestSocket CreateSocket()
|
||||
{
|
||||
Mock.Get(SocketFactory).Setup(f => f.CreateWebsocket(It.IsAny<Log>(), It.IsAny<WebSocketParameters>())).Returns(new TestSocket());
|
||||
return (TestSocket)CreateSocket("https://localhost:123/");
|
||||
Mock.Get(SocketFactory).Setup(f => f.CreateWebsocket(It.IsAny<Log>(), It.IsAny<string>())).Returns(new TestSocket());
|
||||
return (TestSocket)CreateSocket("123");
|
||||
}
|
||||
|
||||
public CallResult<bool> ConnectSocketSub(SocketConnection sub)
|
||||
|
||||
@@ -320,7 +320,7 @@ namespace CryptoExchange.Net
|
||||
responseStream.Close();
|
||||
response.Close();
|
||||
var parseResult = ValidateJson(data);
|
||||
var error = parseResult.Success ? ParseErrorResponse(parseResult.Data) : new ServerError(data)!;
|
||||
var error = parseResult.Success ? ParseErrorResponse(parseResult.Data) : parseResult.Error!;
|
||||
if(error.Code == null || error.Code == 0)
|
||||
error.Code = (int)response.StatusCode;
|
||||
return new WebCallResult<T>(statusCode, headers, sw.Elapsed, data, request.Uri.ToString(), request.Content, request.Method, request.GetHeaders(), default, error);
|
||||
|
||||
@@ -392,14 +392,11 @@ namespace CryptoExchange.Net
|
||||
if (!authenticated || socket.Authenticated)
|
||||
return new CallResult<bool>(true);
|
||||
|
||||
log.Write(LogLevel.Debug, $"Attempting to authenticate {socket.SocketId}");
|
||||
var result = await AuthenticateSocketAsync(socket).ConfigureAwait(false);
|
||||
if (!result)
|
||||
{
|
||||
await socket.CloseAsync().ConfigureAwait(false);
|
||||
log.Write(LogLevel.Warning, $"Socket {socket.SocketId} authentication failed");
|
||||
if(socket.Connected)
|
||||
await socket.CloseAsync().ConfigureAwait(false);
|
||||
|
||||
result.Error!.Message = "Authentication failed: " + result.Error.Message;
|
||||
return new CallResult<bool>(result.Error);
|
||||
}
|
||||
@@ -543,17 +540,6 @@ namespace CryptoExchange.Net
|
||||
return Task.FromResult(new CallResult<string?>(address));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the url to reconnect to after losing a connection
|
||||
/// </summary>
|
||||
/// <param name="apiClient"></param>
|
||||
/// <param name="connection"></param>
|
||||
/// <returns></returns>
|
||||
public virtual Task<Uri?> GetReconnectUriAsync(SocketApiClient apiClient, SocketConnection connection)
|
||||
{
|
||||
return Task.FromResult<Uri?>(connection.ConnectionUri);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a connection for a new subscription or query. Can be an existing if there are open position or a new one.
|
||||
/// </summary>
|
||||
|
||||
@@ -6,16 +6,16 @@
|
||||
<PackageId>CryptoExchange.Net</PackageId>
|
||||
<Authors>JKorf</Authors>
|
||||
<Description>A base package for implementing cryptocurrency API's</Description>
|
||||
<PackageVersion>5.2.3</PackageVersion>
|
||||
<AssemblyVersion>5.2.3</AssemblyVersion>
|
||||
<FileVersion>5.2.3</FileVersion>
|
||||
<PackageVersion>5.2.0</PackageVersion>
|
||||
<AssemblyVersion>5.2.0</AssemblyVersion>
|
||||
<FileVersion>5.2.0</FileVersion>
|
||||
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<RepositoryUrl>https://github.com/JKorf/CryptoExchange.Net.git</RepositoryUrl>
|
||||
<PackageProjectUrl>https://github.com/JKorf/CryptoExchange.Net</PackageProjectUrl>
|
||||
<NeutralLanguage>en</NeutralLanguage>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<PackageReleaseNotes>5.2.3 - Fixed socket getting disconnected when `no data` timeout is reached instead of being reconnected</PackageReleaseNotes>
|
||||
<PackageReleaseNotes>5.2.0 - Refactored websocket code, removed some clutter and simplified, Added ReconnectAsync and GetSubscriptionsState methods on socket clients</PackageReleaseNotes>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>9.0</LangVersion>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using CryptoExchange.Net.Objects;
|
||||
using CryptoExchange.Net.Sockets;
|
||||
using System;
|
||||
using System.Security.Authentication;
|
||||
using System.Text;
|
||||
@@ -36,10 +35,6 @@ namespace CryptoExchange.Net.Interfaces
|
||||
/// Websocket has reconnected to the server
|
||||
/// </summary>
|
||||
event Action OnReconnected;
|
||||
/// <summary>
|
||||
/// Get reconntion url
|
||||
/// </summary>
|
||||
Func<Task<Uri?>> GetReconnectionUrl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Unique id for this socket
|
||||
|
||||
@@ -33,6 +33,7 @@ namespace CryptoExchange.Net.Sockets
|
||||
private readonly AsyncResetEvent _sendEvent;
|
||||
private readonly ConcurrentQueue<byte[]> _sendBuffer;
|
||||
private readonly SemaphoreSlim _closeSem;
|
||||
private readonly WebSocketParameters _parameters;
|
||||
private readonly List<DateTime> _outgoingMessages;
|
||||
|
||||
private ClientWebSocket _socket;
|
||||
@@ -63,16 +64,13 @@ namespace CryptoExchange.Net.Sockets
|
||||
/// <inheritdoc />
|
||||
public int Id { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public WebSocketParameters Parameters { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The timestamp this socket has been active for the last time
|
||||
/// </summary>
|
||||
public DateTime LastActionTime { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public Uri Uri => Parameters.Uri;
|
||||
public Uri Uri => _parameters.Uri;
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsClosed => _socket.State == WebSocketState.Closed;
|
||||
@@ -109,8 +107,6 @@ namespace CryptoExchange.Net.Sockets
|
||||
public event Action? OnReconnecting;
|
||||
/// <inheritdoc />
|
||||
public event Action? OnReconnected;
|
||||
/// <inheritdoc />
|
||||
public Func<Task<Uri?>>? GetReconnectionUrl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// ctor
|
||||
@@ -122,7 +118,7 @@ namespace CryptoExchange.Net.Sockets
|
||||
Id = NextStreamId();
|
||||
_log = log;
|
||||
|
||||
Parameters = websocketParameters;
|
||||
_parameters = websocketParameters;
|
||||
_outgoingMessages = new List<DateTime>();
|
||||
_receivedMessages = new List<ReceiveItem>();
|
||||
_sendEvent = new AsyncResetEvent();
|
||||
@@ -151,17 +147,17 @@ namespace CryptoExchange.Net.Sockets
|
||||
private ClientWebSocket CreateSocket()
|
||||
{
|
||||
var cookieContainer = new CookieContainer();
|
||||
foreach (var cookie in Parameters.Cookies)
|
||||
foreach (var cookie in _parameters.Cookies)
|
||||
cookieContainer.Add(new Cookie(cookie.Key, cookie.Value));
|
||||
|
||||
var socket = new ClientWebSocket();
|
||||
socket.Options.Cookies = cookieContainer;
|
||||
foreach (var header in Parameters.Headers)
|
||||
foreach (var header in _parameters.Headers)
|
||||
socket.Options.SetRequestHeader(header.Key, header.Value);
|
||||
socket.Options.KeepAliveInterval = Parameters.KeepAliveInterval ?? TimeSpan.Zero;
|
||||
socket.Options.KeepAliveInterval = _parameters.KeepAliveInterval ?? TimeSpan.Zero;
|
||||
socket.Options.SetBuffer(65536, 65536); // Setting it to anything bigger than 65536 throws an exception in .net framework
|
||||
if (Parameters.Proxy != null)
|
||||
SetProxy(Parameters.Proxy);
|
||||
if (_parameters.Proxy != null)
|
||||
SetProxy(_parameters.Proxy);
|
||||
return socket;
|
||||
}
|
||||
|
||||
@@ -192,7 +188,7 @@ namespace CryptoExchange.Net.Sockets
|
||||
_processState = ProcessState.Processing;
|
||||
var sendTask = SendLoopAsync();
|
||||
var receiveTask = ReceiveLoopAsync();
|
||||
var timeoutTask = Parameters.Timeout != null && Parameters.Timeout > TimeSpan.FromSeconds(0) ? CheckTimeoutAsync() : Task.CompletedTask;
|
||||
var timeoutTask = _parameters.Timeout != null && _parameters.Timeout > TimeSpan.FromSeconds(0) ? CheckTimeoutAsync() : Task.CompletedTask;
|
||||
await Task.WhenAll(sendTask, receiveTask, timeoutTask).ConfigureAwait(false);
|
||||
_log.Write(LogLevel.Debug, $"Socket {Id} processing tasks finished");
|
||||
|
||||
@@ -203,7 +199,7 @@ namespace CryptoExchange.Net.Sockets
|
||||
await _closeTask.ConfigureAwait(false);
|
||||
_closeTask = null;
|
||||
|
||||
if (!Parameters.AutoReconnect)
|
||||
if (!_parameters.AutoReconnect)
|
||||
{
|
||||
_processState = ProcessState.Idle;
|
||||
OnClose?.Invoke();
|
||||
@@ -213,23 +209,12 @@ namespace CryptoExchange.Net.Sockets
|
||||
if (!_stopRequested)
|
||||
{
|
||||
_processState = ProcessState.Reconnecting;
|
||||
OnReconnecting?.Invoke();
|
||||
OnReconnecting?.Invoke();
|
||||
}
|
||||
|
||||
while (!_stopRequested)
|
||||
{
|
||||
_log.Write(LogLevel.Debug, $"Socket {Id} attempting to reconnect");
|
||||
var task = GetReconnectionUrl?.Invoke();
|
||||
if (task != null)
|
||||
{
|
||||
var reconnectUri = await task.ConfigureAwait(false);
|
||||
if (reconnectUri != null && Parameters.Uri != reconnectUri)
|
||||
{
|
||||
_log.Write(LogLevel.Debug, $"Socket {Id} reconnect URI set to {reconnectUri}");
|
||||
Parameters.Uri = reconnectUri;
|
||||
}
|
||||
}
|
||||
|
||||
_socket = CreateSocket();
|
||||
_ctsSource.Dispose();
|
||||
_ctsSource = new CancellationTokenSource();
|
||||
@@ -238,7 +223,7 @@ namespace CryptoExchange.Net.Sockets
|
||||
var connected = await ConnectInternalAsync().ConfigureAwait(false);
|
||||
if (!connected)
|
||||
{
|
||||
await Task.Delay(Parameters.ReconnectInterval).ConfigureAwait(false);
|
||||
await Task.Delay(_parameters.ReconnectInterval).ConfigureAwait(false);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -256,7 +241,7 @@ namespace CryptoExchange.Net.Sockets
|
||||
if (_ctsSource.IsCancellationRequested)
|
||||
return;
|
||||
|
||||
var bytes = Parameters.Encoding.GetBytes(data);
|
||||
var bytes = _parameters.Encoding.GetBytes(data);
|
||||
_log.Write(LogLevel.Trace, $"Socket {Id} Adding {bytes.Length} to sent buffer");
|
||||
_sendBuffer.Enqueue(bytes);
|
||||
_sendEvent.Set();
|
||||
@@ -265,7 +250,7 @@ namespace CryptoExchange.Net.Sockets
|
||||
/// <inheritdoc />
|
||||
public virtual async Task ReconnectAsync()
|
||||
{
|
||||
if (_processState != ProcessState.Processing && IsOpen)
|
||||
if (_processState != ProcessState.Processing)
|
||||
return;
|
||||
|
||||
_log.Write(LogLevel.Debug, $"Socket {Id} reconnect requested");
|
||||
@@ -385,11 +370,11 @@ namespace CryptoExchange.Net.Sockets
|
||||
|
||||
while (_sendBuffer.TryDequeue(out var data))
|
||||
{
|
||||
if (Parameters.RatelimitPerSecond != null)
|
||||
if (_parameters.RatelimitPerSecond != null)
|
||||
{
|
||||
// Wait for rate limit
|
||||
DateTime? start = null;
|
||||
while (MessagesSentLastSecond() >= Parameters.RatelimitPerSecond)
|
||||
while (MessagesSentLastSecond() >= _parameters.RatelimitPerSecond)
|
||||
{
|
||||
start ??= DateTime.UtcNow;
|
||||
await Task.Delay(50).ConfigureAwait(false);
|
||||
@@ -564,14 +549,14 @@ namespace CryptoExchange.Net.Sockets
|
||||
string strData;
|
||||
if (messageType == WebSocketMessageType.Binary)
|
||||
{
|
||||
if (Parameters.DataInterpreterBytes == null)
|
||||
if (_parameters.DataInterpreterBytes == null)
|
||||
throw new Exception("Byte interpreter not set while receiving byte data");
|
||||
|
||||
try
|
||||
{
|
||||
var relevantData = new byte[count];
|
||||
Array.Copy(data, offset, relevantData, 0, count);
|
||||
strData = Parameters.DataInterpreterBytes(relevantData);
|
||||
strData = _parameters.DataInterpreterBytes(relevantData);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
@@ -580,13 +565,13 @@ namespace CryptoExchange.Net.Sockets
|
||||
}
|
||||
}
|
||||
else
|
||||
strData = Parameters.Encoding.GetString(data, offset, count);
|
||||
strData = _parameters.Encoding.GetString(data, offset, count);
|
||||
|
||||
if (Parameters.DataInterpreterString != null)
|
||||
if (_parameters.DataInterpreterString != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
strData = Parameters.DataInterpreterString(strData);
|
||||
strData = _parameters.DataInterpreterString(strData);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
@@ -648,7 +633,7 @@ namespace CryptoExchange.Net.Sockets
|
||||
/// <returns></returns>
|
||||
protected async Task CheckTimeoutAsync()
|
||||
{
|
||||
_log.Write(LogLevel.Debug, $"Socket {Id} Starting task checking for no data received for {Parameters.Timeout}");
|
||||
_log.Write(LogLevel.Debug, $"Socket {Id} Starting task checking for no data received for {_parameters.Timeout}");
|
||||
LastActionTime = DateTime.UtcNow;
|
||||
try
|
||||
{
|
||||
@@ -657,10 +642,10 @@ namespace CryptoExchange.Net.Sockets
|
||||
if (_ctsSource.IsCancellationRequested)
|
||||
return;
|
||||
|
||||
if (DateTime.UtcNow - LastActionTime > Parameters.Timeout)
|
||||
if (DateTime.UtcNow - LastActionTime > _parameters.Timeout)
|
||||
{
|
||||
_log.Write(LogLevel.Warning, $"Socket {Id} No data received for {Parameters.Timeout}, reconnecting socket");
|
||||
_ = ReconnectAsync().ConfigureAwait(false);
|
||||
_log.Write(LogLevel.Warning, $"Socket {Id} No data received for {_parameters.Timeout}, reconnecting socket");
|
||||
_ = CloseAsync().ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
try
|
||||
|
||||
@@ -184,7 +184,6 @@ namespace CryptoExchange.Net.Sockets
|
||||
_socket.OnReconnecting += HandleReconnecting;
|
||||
_socket.OnReconnected += HandleReconnected;
|
||||
_socket.OnError += HandleError;
|
||||
_socket.GetReconnectionUrl = GetReconnectionUrlAsync;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -224,17 +223,7 @@ namespace CryptoExchange.Net.Sockets
|
||||
foreach (var sub in subscriptions)
|
||||
sub.Confirmed = false;
|
||||
}
|
||||
|
||||
_ = Task.Run(() => ConnectionLost?.Invoke());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the url to connect to when reconnecting
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected virtual async Task<Uri?> GetReconnectionUrlAsync()
|
||||
{
|
||||
return await socketClient.GetReconnectUriAsync(ApiClient, this).ConfigureAwait(false);
|
||||
Task.Run(() => ConnectionLost?.Invoke());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -396,7 +385,7 @@ namespace CryptoExchange.Net.Sockets
|
||||
if (!subscriptions.Contains(subscription))
|
||||
return;
|
||||
|
||||
subscription.Closed = true;
|
||||
subscriptions.Remove(subscription);
|
||||
}
|
||||
|
||||
if (Status == SocketStatus.Closing || Status == SocketStatus.Closed || Status == SocketStatus.Disposed)
|
||||
@@ -418,7 +407,7 @@ namespace CryptoExchange.Net.Sockets
|
||||
return;
|
||||
}
|
||||
|
||||
shouldCloseConnection = subscriptions.All(r => !r.UserSubscription || r.Closed);
|
||||
shouldCloseConnection = subscriptions.All(r => !r.UserSubscription);
|
||||
if (shouldCloseConnection)
|
||||
Status = SocketStatus.Closing;
|
||||
}
|
||||
@@ -428,9 +417,6 @@ namespace CryptoExchange.Net.Sockets
|
||||
log.Write(LogLevel.Debug, $"Socket {SocketId} closing as there are no more subscriptions");
|
||||
await CloseAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
lock (subscriptionLock)
|
||||
subscriptions.Remove(subscription);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -48,11 +48,6 @@ namespace CryptoExchange.Net.Sockets
|
||||
/// </summary>
|
||||
public bool Authenticated { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether we're closing this subscription and a socket connection shouldn't be kept open for it
|
||||
/// </summary>
|
||||
public bool Closed { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Cancellation token registration, should be disposed when subscription is closed. Used for closing the subscription with
|
||||
/// a provided cancelation token
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CryptoExchange.Net.Sockets
|
||||
{
|
||||
|
||||
@@ -18,17 +18,6 @@ I develop and maintain this package on my own for free in my spare time. Donatio
|
||||
Alternatively, sponsor me on Github using [Github Sponsors](https://github.com/sponsors/JKorf)
|
||||
|
||||
## Release notes
|
||||
* Version 5.2.3 - 19 Jul 2022
|
||||
* Fixed socket getting disconnected when `no data` timeout is reached instead of being reconnected
|
||||
|
||||
* Version 5.2.2 - 17 Jul 2022
|
||||
* Added support for retrieving a new url when socket connection is lost and reconnection will happen
|
||||
|
||||
* Version 5.2.1 - 16 Jul 2022
|
||||
* Fixed socket reconnect issue
|
||||
* Fixed `message not handled` messages after unsubscribing
|
||||
* Fixed error returning for non-json error responses
|
||||
|
||||
* Version 5.2.0 - 10 Jul 2022
|
||||
* Refactored websocket code, removed some clutter and simplified
|
||||
* Added ReconnectAsync and GetSubscriptionsState methods on socket clients
|
||||
|
||||
Reference in New Issue
Block a user