1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-08 16:36:15 +00:00
This commit is contained in:
Jkorf 2021-09-14 08:24:11 +02:00
commit 31d0122096
5 changed files with 81 additions and 18 deletions

View File

@ -3296,6 +3296,13 @@
<param name="interval">How often</param> <param name="interval">How often</param>
<param name="objGetter">Method returning the object to send</param> <param name="objGetter">Method returning the object to send</param>
</member> </member>
<member name="M:CryptoExchange.Net.SocketClient.UnsubscribeAsync(System.Int32)">
<summary>
Unsubscribe an update subscription
</summary>
<param name="subscriptionId">The id of the subscription to unsubscribe</param>
<returns></returns>
</member>
<member name="M:CryptoExchange.Net.SocketClient.UnsubscribeAsync(CryptoExchange.Net.Sockets.UpdateSubscription)"> <member name="M:CryptoExchange.Net.SocketClient.UnsubscribeAsync(CryptoExchange.Net.Sockets.UpdateSubscription)">
<summary> <summary>
Unsubscribe an update subscription Unsubscribe an update subscription
@ -3718,6 +3725,12 @@
</summary> </summary>
<param name="subscription"></param> <param name="subscription"></param>
</member> </member>
<member name="M:CryptoExchange.Net.Sockets.SocketConnection.GetSubscription(System.Int32)">
<summary>
Get a subscription on this connection
</summary>
<param name="id"></param>
</member>
<member name="M:CryptoExchange.Net.Sockets.SocketConnection.SendAndWaitAsync``1(``0,System.TimeSpan,System.Func{Newtonsoft.Json.Linq.JToken,System.Boolean})"> <member name="M:CryptoExchange.Net.Sockets.SocketConnection.SendAndWaitAsync``1(``0,System.TimeSpan,System.Func{Newtonsoft.Json.Linq.JToken,System.Boolean})">
<summary> <summary>
Send data and wait for an answer Send data and wait for an answer
@ -3795,19 +3808,21 @@
If the subscription has been confirmed If the subscription has been confirmed
</summary> </summary>
</member> </member>
<member name="M:CryptoExchange.Net.Sockets.SocketSubscription.CreateForRequest(System.Object,System.Boolean,System.Action{CryptoExchange.Net.Sockets.MessageEvent})"> <member name="M:CryptoExchange.Net.Sockets.SocketSubscription.CreateForRequest(System.Int32,System.Object,System.Boolean,System.Action{CryptoExchange.Net.Sockets.MessageEvent})">
<summary> <summary>
Create SocketSubscription for a request Create SocketSubscription for a request
</summary> </summary>
<param name="id"></param>
<param name="request"></param> <param name="request"></param>
<param name="userSubscription"></param> <param name="userSubscription"></param>
<param name="dataHandler"></param> <param name="dataHandler"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:CryptoExchange.Net.Sockets.SocketSubscription.CreateForIdentifier(System.String,System.Boolean,System.Action{CryptoExchange.Net.Sockets.MessageEvent})"> <member name="M:CryptoExchange.Net.Sockets.SocketSubscription.CreateForIdentifier(System.Int32,System.String,System.Boolean,System.Action{CryptoExchange.Net.Sockets.MessageEvent})">
<summary> <summary>
Create SocketSubscription for an identifier Create SocketSubscription for an identifier
</summary> </summary>
<param name="id"></param>
<param name="identifier"></param> <param name="identifier"></param>
<param name="userSubscription"></param> <param name="userSubscription"></param>
<param name="dataHandler"></param> <param name="dataHandler"></param>
@ -3857,11 +3872,16 @@
Event when an exception happens during the handling of the data Event when an exception happens during the handling of the data
</summary> </summary>
</member> </member>
<member name="P:CryptoExchange.Net.Sockets.UpdateSubscription.Id"> <member name="P:CryptoExchange.Net.Sockets.UpdateSubscription.SocketId">
<summary> <summary>
The id of the socket The id of the socket
</summary> </summary>
</member> </member>
<member name="P:CryptoExchange.Net.Sockets.UpdateSubscription.Id">
<summary>
The id of the subscription
</summary>
</member>
<member name="M:CryptoExchange.Net.Sockets.UpdateSubscription.#ctor(CryptoExchange.Net.Sockets.SocketConnection,CryptoExchange.Net.Sockets.SocketSubscription)"> <member name="M:CryptoExchange.Net.Sockets.UpdateSubscription.#ctor(CryptoExchange.Net.Sockets.SocketConnection,CryptoExchange.Net.Sockets.SocketSubscription)">
<summary> <summary>
ctor ctor
@ -3904,9 +3924,7 @@
<member name="M:CryptoExchange.Net.Sockets.WebsocketFactory.CreateWebsocket(CryptoExchange.Net.Logging.Log,System.String,System.Collections.Generic.IDictionary{System.String,System.String},System.Collections.Generic.IDictionary{System.String,System.String})"> <member name="M:CryptoExchange.Net.Sockets.WebsocketFactory.CreateWebsocket(CryptoExchange.Net.Logging.Log,System.String,System.Collections.Generic.IDictionary{System.String,System.String},System.Collections.Generic.IDictionary{System.String,System.String})">
<inheritdoc /> <inheritdoc />
</member> </member>
</members> <member name="T:System.Diagnostics.CodeAnalysis.AllowNullAttribute">
</doc>
System.Diagnostics.CodeAnalysis.AllowNullAttribute">
<summary> <summary>
Specifies that <see langword="null"/> is allowed as an input even if the Specifies that <see langword="null"/> is allowed as an input even if the
corresponding type disallows it. corresponding type disallows it.

View File

@ -443,8 +443,8 @@ namespace CryptoExchange.Net
} }
var subscription = request == null var subscription = request == null
? SocketSubscription.CreateForIdentifier(identifier!, userSubscription, InternalHandler) ? SocketSubscription.CreateForIdentifier(NextId(), identifier!, userSubscription, InternalHandler)
: SocketSubscription.CreateForRequest(request, userSubscription, InternalHandler); : SocketSubscription.CreateForRequest(NextId(), request, userSubscription, InternalHandler);
connection.AddSubscription(subscription); connection.AddSubscription(subscription);
return subscription; return subscription;
} }
@ -457,7 +457,7 @@ namespace CryptoExchange.Net
protected void AddGenericHandler(string identifier, Action<MessageEvent> action) protected void AddGenericHandler(string identifier, Action<MessageEvent> action)
{ {
genericHandlers.Add(identifier, 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) foreach (var connection in sockets.Values)
connection.AddSubscription(subscription); connection.AddSubscription(subscription);
} }
@ -488,7 +488,7 @@ namespace CryptoExchange.Net
socketConnection.UnhandledMessage += HandleUnhandledMessage; socketConnection.UnhandledMessage += HandleUnhandledMessage;
foreach (var kvp in genericHandlers) 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); socketConnection.AddSubscription(handler);
} }
@ -590,7 +590,33 @@ namespace CryptoExchange.Net
} }
}); });
} }
/// <summary>
/// Unsubscribe an update subscription
/// </summary>
/// <param name="subscriptionId">The id of the subscription to unsubscribe</param>
/// <returns></returns>
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);
}
/// <summary> /// <summary>
/// Unsubscribe an update subscription /// Unsubscribe an update subscription
@ -602,7 +628,7 @@ namespace CryptoExchange.Net
if (subscription == null) if (subscription == null)
throw new ArgumentNullException(nameof(subscription)); throw new ArgumentNullException(nameof(subscription));
log.Write(LogLevel.Information, "Closing subscription"); log.Write(LogLevel.Information, "Closing subscription " + subscription.Id);
await subscription.CloseAsync().ConfigureAwait(false); await subscription.CloseAsync().ConfigureAwait(false);
} }

View File

@ -205,6 +205,16 @@ namespace CryptoExchange.Net.Sockets
subscriptions.Add(subscription); subscriptions.Add(subscription);
} }
/// <summary>
/// Get a subscription on this connection
/// </summary>
/// <param name="id"></param>
public SocketSubscription GetSubscription(int id)
{
lock (subscriptionLock)
return subscriptions.SingleOrDefault(s => s.Id == id);
}
private bool HandleData(MessageEvent messageEvent) private bool HandleData(MessageEvent messageEvent)
{ {
SocketSubscription? currentSubscription = null; SocketSubscription? currentSubscription = null;

View File

@ -7,6 +7,7 @@ namespace CryptoExchange.Net.Sockets
/// </summary> /// </summary>
public class SocketSubscription public class SocketSubscription
{ {
public int Id { get; }
/// <summary> /// <summary>
/// Exception event /// Exception event
/// </summary> /// </summary>
@ -35,8 +36,9 @@ namespace CryptoExchange.Net.Sockets
/// </summary> /// </summary>
public bool Confirmed { get; set; } public bool Confirmed { get; set; }
private SocketSubscription(object? request, string? identifier, bool userSubscription, Action<MessageEvent> dataHandler) private SocketSubscription(int id, object? request, string? identifier, bool userSubscription, Action<MessageEvent> dataHandler)
{ {
Id = id;
UserSubscription = userSubscription; UserSubscription = userSubscription;
MessageHandler = dataHandler; MessageHandler = dataHandler;
Request = request; Request = request;
@ -46,27 +48,29 @@ namespace CryptoExchange.Net.Sockets
/// <summary> /// <summary>
/// Create SocketSubscription for a request /// Create SocketSubscription for a request
/// </summary> /// </summary>
/// <param name="id"></param>
/// <param name="request"></param> /// <param name="request"></param>
/// <param name="userSubscription"></param> /// <param name="userSubscription"></param>
/// <param name="dataHandler"></param> /// <param name="dataHandler"></param>
/// <returns></returns> /// <returns></returns>
public static SocketSubscription CreateForRequest(object request, bool userSubscription, public static SocketSubscription CreateForRequest(int id, object request, bool userSubscription,
Action<MessageEvent> dataHandler) Action<MessageEvent> dataHandler)
{ {
return new SocketSubscription(request, null, userSubscription, dataHandler); return new SocketSubscription(id, request, null, userSubscription, dataHandler);
} }
/// <summary> /// <summary>
/// Create SocketSubscription for an identifier /// Create SocketSubscription for an identifier
/// </summary> /// </summary>
/// <param name="id"></param>
/// <param name="identifier"></param> /// <param name="identifier"></param>
/// <param name="userSubscription"></param> /// <param name="userSubscription"></param>
/// <param name="dataHandler"></param> /// <param name="dataHandler"></param>
/// <returns></returns> /// <returns></returns>
public static SocketSubscription CreateForIdentifier(string identifier, bool userSubscription, public static SocketSubscription CreateForIdentifier(int id, string identifier, bool userSubscription,
Action<MessageEvent> dataHandler) Action<MessageEvent> dataHandler)
{ {
return new SocketSubscription(null, identifier, userSubscription, dataHandler); return new SocketSubscription(id, null, identifier, userSubscription, dataHandler);
} }
/// <summary> /// <summary>

View File

@ -72,7 +72,12 @@ namespace CryptoExchange.Net.Sockets
/// <summary> /// <summary>
/// The id of the socket /// The id of the socket
/// </summary> /// </summary>
public int Id => connection.Socket.Id; public int SocketId => connection.Socket.Id;
/// <summary>
/// The id of the subscription
/// </summary>
public int Id => subscription.Id;
/// <summary> /// <summary>
/// ctor /// ctor