mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-11 01:46:12 +00:00
Added name to message handlers, added socket type to subscription for background sockets
This commit is contained in:
parent
8611723c6f
commit
1b6e1f99f8
@ -17,4 +17,11 @@
|
|||||||
FormData,
|
FormData,
|
||||||
Json
|
Json
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum SocketType
|
||||||
|
{
|
||||||
|
Normal,
|
||||||
|
Background,
|
||||||
|
BackgroundAuthenticated
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,15 @@ namespace CryptoExchange.Net
|
|||||||
|
|
||||||
protected TimeSpan reconnectInterval;
|
protected TimeSpan reconnectInterval;
|
||||||
protected Func<byte[], string> dataInterpreter;
|
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
|
#endregion
|
||||||
|
|
||||||
protected SocketClient(SocketClientOptions exchangeOptions, AuthenticationProvider authenticationProvider): base(exchangeOptions, authenticationProvider)
|
protected SocketClient(SocketClientOptions exchangeOptions, AuthenticationProvider authenticationProvider): base(exchangeOptions, authenticationProvider)
|
||||||
@ -84,6 +93,11 @@ namespace CryptoExchange.Net
|
|||||||
return socket;
|
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 SocketOpened(IWebsocket socket) { }
|
||||||
protected virtual void SocketClosed(IWebsocket socket) { }
|
protected virtual void SocketClosed(IWebsocket socket) { }
|
||||||
protected virtual void SocketError(IWebsocket socket, Exception ex) { }
|
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);
|
log.Write(LogVerbosity.Debug, $"Socket {subscription.Socket.Id} received data: " + data);
|
||||||
foreach (var handler in subscription.MessageHandlers)
|
foreach (var handler in subscription.MessageHandlers)
|
||||||
if (handler(subscription, JToken.Parse(data)))
|
if (handler.Value(subscription, JToken.Parse(data)))
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,12 +16,14 @@ namespace CryptoExchange.Net.Sockets
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Message handlers for this subscription. Should return true if the message is handled and should not be distributed to the other handlers
|
/// Message handlers for this subscription. Should return true if the message is handled and should not be distributed to the other handlers
|
||||||
/// </summary>
|
/// </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 List<SocketEvent> Events { get; set; }
|
||||||
|
|
||||||
public IWebsocket Socket { get; set; }
|
public IWebsocket Socket { get; set; }
|
||||||
public SocketRequest Request { get; set; }
|
public SocketRequest Request { get; set; }
|
||||||
|
|
||||||
|
public SocketType Type { get; set; }
|
||||||
|
|
||||||
private bool lostTriggered;
|
private bool lostTriggered;
|
||||||
private List<SocketEvent> waitingForEvents;
|
private List<SocketEvent> waitingForEvents;
|
||||||
|
|
||||||
@ -32,7 +34,7 @@ namespace CryptoExchange.Net.Sockets
|
|||||||
Events = new List<SocketEvent>();
|
Events = new List<SocketEvent>();
|
||||||
waitingForEvents = new List<SocketEvent>();
|
waitingForEvents = new List<SocketEvent>();
|
||||||
|
|
||||||
MessageHandlers = new List<Func<SocketSubscription, JToken, bool>>();
|
MessageHandlers = new Dictionary<string, Func<SocketSubscription, JToken, bool>>();
|
||||||
|
|
||||||
Socket.OnClose += () =>
|
Socket.OnClose += () =>
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user