1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-10 09:26:22 +00:00

Added name to message handlers, added socket type to subscription for background sockets

This commit is contained in:
JKorf 2018-11-29 11:28:46 +01:00
parent 8611723c6f
commit 1b6e1f99f8
3 changed files with 26 additions and 3 deletions

View File

@ -17,4 +17,11 @@
FormData,
Json
}
public enum SocketType
{
Normal,
Background,
BackgroundAuthenticated
}
}

View File

@ -26,6 +26,15 @@ namespace CryptoExchange.Net
protected TimeSpan reconnectInterval;
protected Func<byte[], string> dataInterpreter;
protected const string DataHandlerName = "DataHandler";
protected const string AuthenticationHandlerName = "AuthenticationHandler";
protected const string SubscriptionHandlerName = "SubscriptionHandler";
protected const string PingHandlerName = "SubscriptionHandler";
protected const string DataEvent = "Data";
protected const string SubscriptionEvent = "Subscription";
protected const string AuthenticationEvent = "Authentication";
#endregion
protected SocketClient(SocketClientOptions exchangeOptions, AuthenticationProvider authenticationProvider): base(exchangeOptions, authenticationProvider)
@ -84,6 +93,11 @@ namespace CryptoExchange.Net
return socket;
}
protected virtual SocketSubscription GetBackgroundSocket(bool authenticated = false)
{
return sockets.SingleOrDefault(s => s.Type == (authenticated ? SocketType.BackgroundAuthenticated : SocketType.Background));
}
protected virtual void SocketOpened(IWebsocket socket) { }
protected virtual void SocketClosed(IWebsocket socket) { }
protected virtual void SocketError(IWebsocket socket, Exception ex) { }
@ -125,7 +139,7 @@ namespace CryptoExchange.Net
{
log.Write(LogVerbosity.Debug, $"Socket {subscription.Socket.Id} received data: " + data);
foreach (var handler in subscription.MessageHandlers)
if (handler(subscription, JToken.Parse(data)))
if (handler.Value(subscription, JToken.Parse(data)))
return;
}

View File

@ -16,12 +16,14 @@ namespace CryptoExchange.Net.Sockets
/// <summary>
/// Message handlers for this subscription. Should return true if the message is handled and should not be distributed to the other handlers
/// </summary>
public List<Func<SocketSubscription, JToken, bool>> MessageHandlers { get; set; }
public Dictionary<string, Func<SocketSubscription, JToken, bool>> MessageHandlers { get; set; }
public List<SocketEvent> Events { get; set; }
public IWebsocket Socket { get; set; }
public SocketRequest Request { get; set; }
public SocketType Type { get; set; }
private bool lostTriggered;
private List<SocketEvent> waitingForEvents;
@ -32,7 +34,7 @@ namespace CryptoExchange.Net.Sockets
Events = new List<SocketEvent>();
waitingForEvents = new List<SocketEvent>();
MessageHandlers = new List<Func<SocketSubscription, JToken, bool>>();
MessageHandlers = new Dictionary<string, Func<SocketSubscription, JToken, bool>>();
Socket.OnClose += () =>
{