diff --git a/CryptoExchange.Net/CryptoExchange.Net.xml b/CryptoExchange.Net/CryptoExchange.Net.xml
index dc059e3..9b2d7ad 100644
--- a/CryptoExchange.Net/CryptoExchange.Net.xml
+++ b/CryptoExchange.Net/CryptoExchange.Net.xml
@@ -3296,6 +3296,13 @@
How often
Method returning the object to send
+
+
+ Unsubscribe an update subscription
+
+ The id of the subscription to unsubscribe
+
+
Unsubscribe an update subscription
@@ -3718,6 +3725,12 @@
+
+
+ Get a subscription on this connection
+
+
+
Send data and wait for an answer
@@ -3795,19 +3808,21 @@
If the subscription has been confirmed
-
+
Create SocketSubscription for a request
+
-
+
Create SocketSubscription for an identifier
+
@@ -3857,11 +3872,16 @@
Event when an exception happens during the handling of the data
-
+
The id of the socket
+
+
+ The id of the subscription
+
+
ctor
@@ -3904,9 +3924,7 @@
-
-
-System.Diagnostics.CodeAnalysis.AllowNullAttribute">
+
Specifies that is allowed as an input even if the
corresponding type disallows it.
diff --git a/CryptoExchange.Net/SocketClient.cs b/CryptoExchange.Net/SocketClient.cs
index 73604ec..f277c43 100644
--- a/CryptoExchange.Net/SocketClient.cs
+++ b/CryptoExchange.Net/SocketClient.cs
@@ -443,8 +443,8 @@ namespace CryptoExchange.Net
}
var subscription = request == null
- ? SocketSubscription.CreateForIdentifier(identifier!, userSubscription, InternalHandler)
- : SocketSubscription.CreateForRequest(request, userSubscription, InternalHandler);
+ ? SocketSubscription.CreateForIdentifier(NextId(), identifier!, userSubscription, InternalHandler)
+ : SocketSubscription.CreateForRequest(NextId(), request, userSubscription, InternalHandler);
connection.AddSubscription(subscription);
return subscription;
}
@@ -457,7 +457,7 @@ namespace CryptoExchange.Net
protected void AddGenericHandler(string identifier, Action action)
{
genericHandlers.Add(identifier, action);
- var subscription = SocketSubscription.CreateForIdentifier(identifier, false, action);
+ var subscription = SocketSubscription.CreateForIdentifier(NextId(), identifier, false, action);
foreach (var connection in sockets.Values)
connection.AddSubscription(subscription);
}
@@ -488,7 +488,7 @@ namespace CryptoExchange.Net
socketConnection.UnhandledMessage += HandleUnhandledMessage;
foreach (var kvp in genericHandlers)
{
- var handler = SocketSubscription.CreateForIdentifier(kvp.Key, false, kvp.Value);
+ var handler = SocketSubscription.CreateForIdentifier(NextId(), kvp.Key, false, kvp.Value);
socketConnection.AddSubscription(handler);
}
@@ -590,7 +590,33 @@ namespace CryptoExchange.Net
}
});
}
-
+
+ ///
+ /// Unsubscribe an update subscription
+ ///
+ /// The id of the subscription to unsubscribe
+ ///
+ public virtual async Task UnsubscribeAsync(int subscriptionId)
+ {
+
+ SocketSubscription? subscription = null;
+ SocketConnection? connection = null;
+ foreach(var socket in sockets.Values.ToList())
+ {
+ subscription = socket.GetSubscription(subscriptionId);
+ if (subscription != null)
+ {
+ connection = socket;
+ break;
+ }
+ }
+
+ if (subscription == null || connection == null)
+ return;
+
+ log.Write(LogLevel.Information, "Closing subscription " + subscriptionId);
+ await connection.CloseAsync(subscription).ConfigureAwait(false);
+ }
///
/// Unsubscribe an update subscription
@@ -602,7 +628,7 @@ namespace CryptoExchange.Net
if (subscription == null)
throw new ArgumentNullException(nameof(subscription));
- log.Write(LogLevel.Information, "Closing subscription");
+ log.Write(LogLevel.Information, "Closing subscription " + subscription.Id);
await subscription.CloseAsync().ConfigureAwait(false);
}
diff --git a/CryptoExchange.Net/Sockets/SocketConnection.cs b/CryptoExchange.Net/Sockets/SocketConnection.cs
index 4d66804..02ec83c 100644
--- a/CryptoExchange.Net/Sockets/SocketConnection.cs
+++ b/CryptoExchange.Net/Sockets/SocketConnection.cs
@@ -205,6 +205,16 @@ namespace CryptoExchange.Net.Sockets
subscriptions.Add(subscription);
}
+ ///
+ /// Get a subscription on this connection
+ ///
+ ///
+ public SocketSubscription GetSubscription(int id)
+ {
+ lock (subscriptionLock)
+ return subscriptions.SingleOrDefault(s => s.Id == id);
+ }
+
private bool HandleData(MessageEvent messageEvent)
{
SocketSubscription? currentSubscription = null;
diff --git a/CryptoExchange.Net/Sockets/SocketSubscription.cs b/CryptoExchange.Net/Sockets/SocketSubscription.cs
index 9481bc4..4fcb816 100644
--- a/CryptoExchange.Net/Sockets/SocketSubscription.cs
+++ b/CryptoExchange.Net/Sockets/SocketSubscription.cs
@@ -7,6 +7,7 @@ namespace CryptoExchange.Net.Sockets
///
public class SocketSubscription
{
+ public int Id { get; }
///
/// Exception event
///
@@ -35,8 +36,9 @@ namespace CryptoExchange.Net.Sockets
///
public bool Confirmed { get; set; }
- private SocketSubscription(object? request, string? identifier, bool userSubscription, Action dataHandler)
+ private SocketSubscription(int id, object? request, string? identifier, bool userSubscription, Action dataHandler)
{
+ Id = id;
UserSubscription = userSubscription;
MessageHandler = dataHandler;
Request = request;
@@ -46,27 +48,29 @@ namespace CryptoExchange.Net.Sockets
///
/// Create SocketSubscription for a request
///
+ ///
///
///
///
///
- public static SocketSubscription CreateForRequest(object request, bool userSubscription,
+ public static SocketSubscription CreateForRequest(int id, object request, bool userSubscription,
Action dataHandler)
{
- return new SocketSubscription(request, null, userSubscription, dataHandler);
+ return new SocketSubscription(id, request, null, userSubscription, dataHandler);
}
///
/// Create SocketSubscription for an identifier
///
+ ///
///
///
///
///
- public static SocketSubscription CreateForIdentifier(string identifier, bool userSubscription,
+ public static SocketSubscription CreateForIdentifier(int id, string identifier, bool userSubscription,
Action dataHandler)
{
- return new SocketSubscription(null, identifier, userSubscription, dataHandler);
+ return new SocketSubscription(id, null, identifier, userSubscription, dataHandler);
}
///
diff --git a/CryptoExchange.Net/Sockets/UpdateSubscription.cs b/CryptoExchange.Net/Sockets/UpdateSubscription.cs
index b3143ef..dd8b029 100644
--- a/CryptoExchange.Net/Sockets/UpdateSubscription.cs
+++ b/CryptoExchange.Net/Sockets/UpdateSubscription.cs
@@ -72,7 +72,12 @@ namespace CryptoExchange.Net.Sockets
///
/// The id of the socket
///
- public int Id => connection.Socket.Id;
+ public int SocketId => connection.Socket.Id;
+
+ ///
+ /// The id of the subscription
+ ///
+ public int Id => subscription.Id;
///
/// ctor