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="objGetter">Method returning the object to send</param>
</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)">
<summary>
Unsubscribe an update subscription
@ -3718,6 +3725,12 @@
</summary>
<param name="subscription"></param>
</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})">
<summary>
Send data and wait for an answer
@ -3795,19 +3808,21 @@
If the subscription has been confirmed
</summary>
</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>
Create SocketSubscription for a request
</summary>
<param name="id"></param>
<param name="request"></param>
<param name="userSubscription"></param>
<param name="dataHandler"></param>
<returns></returns>
</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>
Create SocketSubscription for an identifier
</summary>
<param name="id"></param>
<param name="identifier"></param>
<param name="userSubscription"></param>
<param name="dataHandler"></param>
@ -3857,11 +3872,16 @@
Event when an exception happens during the handling of the data
</summary>
</member>
<member name="P:CryptoExchange.Net.Sockets.UpdateSubscription.Id">
<member name="P:CryptoExchange.Net.Sockets.UpdateSubscription.SocketId">
<summary>
The id of the socket
</summary>
</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)">
<summary>
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})">
<inheritdoc />
</member>
</members>
</doc>
System.Diagnostics.CodeAnalysis.AllowNullAttribute">
<member name="T:System.Diagnostics.CodeAnalysis.AllowNullAttribute">
<summary>
Specifies that <see langword="null"/> is allowed as an input even if the
corresponding type disallows it.

View File

@ -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<MessageEvent> 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);
}
@ -591,6 +591,32 @@ 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>
/// 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);
}

View File

@ -205,6 +205,16 @@ namespace CryptoExchange.Net.Sockets
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)
{
SocketSubscription? currentSubscription = null;

View File

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

View File

@ -72,7 +72,12 @@ namespace CryptoExchange.Net.Sockets
/// <summary>
/// The id of the socket
/// </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>
/// ctor