diff --git a/CryptoExchange.Net/Clients/SocketApiClient.cs b/CryptoExchange.Net/Clients/SocketApiClient.cs index 5f6b25b..ba9d763 100644 --- a/CryptoExchange.Net/Clients/SocketApiClient.cs +++ b/CryptoExchange.Net/Clients/SocketApiClient.cs @@ -72,6 +72,11 @@ namespace CryptoExchange.Net.Clients /// protected List DedicatedConnectionConfigs { get; set; } = new List(); + /// + /// Whether to allow multiple subscriptions with the same topic on the same connection + /// + protected bool AllowTopicsOnTheSameConnection { get; set; } = true; + /// public double IncomingKbps { @@ -211,7 +216,7 @@ namespace CryptoExchange.Net.Clients while (true) { // Get a new or existing socket connection - var socketResult = await GetSocketConnection(url, subscription.Authenticated, false).ConfigureAwait(false); + var socketResult = await GetSocketConnection(url, subscription.Authenticated, false, subscription.Topic).ConfigureAwait(false); if (!socketResult) return socketResult.As(null); @@ -478,13 +483,15 @@ namespace CryptoExchange.Net.Clients /// The address the socket is for /// Whether the socket should be authenticated /// Whether a dedicated request connection should be returned + /// The subscription topic, can be provided when multiple of the same topics are not allowed on a connection /// - protected virtual async Task> GetSocketConnection(string address, bool authenticated, bool dedicatedRequestConnection) + protected virtual async Task> GetSocketConnection(string address, bool authenticated, bool dedicatedRequestConnection, string? topic = null) { var socketQuery = socketConnections.Where(s => (s.Value.Status == SocketConnection.SocketStatus.None || s.Value.Status == SocketConnection.SocketStatus.Connected) && s.Value.Tag.TrimEnd('/') == address.TrimEnd('/') && s.Value.ApiClient.GetType() == GetType() && (s.Value.Authenticated == authenticated || !authenticated) + && (AllowTopicsOnTheSameConnection || !s.Value.Topics.Contains(topic)) && s.Value.Connected); SocketConnection connection; diff --git a/CryptoExchange.Net/Sockets/SocketConnection.cs b/CryptoExchange.Net/Sockets/SocketConnection.cs index 755df4b..93b19b6 100644 --- a/CryptoExchange.Net/Sockets/SocketConnection.cs +++ b/CryptoExchange.Net/Sockets/SocketConnection.cs @@ -190,6 +190,18 @@ namespace CryptoExchange.Net.Sockets /// public DedicatedConnectionState DedicatedRequestConnection { get; internal set; } = new DedicatedConnectionState(); + /// + /// Current subscription topics on this connection + /// + public IEnumerable Topics + { + get + { + lock (_listenersLock) + return _listeners.OfType().Select(x => x.Topic).Where(t => t != null).ToList()!; + } + } + private bool _pausedActivity; private readonly object _listenersLock; private readonly List _listeners; diff --git a/CryptoExchange.Net/Sockets/Subscription.cs b/CryptoExchange.Net/Sockets/Subscription.cs index 2f12581..ef3dee3 100644 --- a/CryptoExchange.Net/Sockets/Subscription.cs +++ b/CryptoExchange.Net/Sockets/Subscription.cs @@ -76,6 +76,11 @@ namespace CryptoExchange.Net.Sockets /// public abstract Type? GetMessageType(IMessageAccessor message); + /// + /// Subscription topic + /// + public string? Topic { get; set; } + /// /// ctor ///