1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-08 00:16:27 +00:00

Small basesocket changes, added total request prop to rest client

This commit is contained in:
JKorf 2019-01-09 11:40:13 +01:00
parent 2b1c77577c
commit b5a80a3095
5 changed files with 17 additions and 4 deletions

View File

@ -13,6 +13,7 @@ namespace CryptoExchange.Net.Interfaces
event Action OnOpen; event Action OnOpen;
int Id { get; } int Id { get; }
string Origin { get; set; }
bool ShouldReconnect { get; set; } bool ShouldReconnect { get; set; }
bool Reconnecting { get; set; } bool Reconnecting { get; set; }
Func<byte[], string> DataInterpreter { get; set; } Func<byte[], string> DataInterpreter { get; set; }

View File

@ -1,9 +1,11 @@
using CryptoExchange.Net.Logging; using System.Collections.Generic;
using CryptoExchange.Net.Logging;
namespace CryptoExchange.Net.Interfaces namespace CryptoExchange.Net.Interfaces
{ {
public interface IWebsocketFactory public interface IWebsocketFactory
{ {
IWebsocket CreateWebsocket(Log log, string url); IWebsocket CreateWebsocket(Log log, string url);
IWebsocket CreateWebsocket(Log log, string url, IDictionary<string, string> cookies, IDictionary<string, string> headers);
} }
} }

View File

@ -33,6 +33,7 @@ namespace CryptoExchange.Net
protected TimeSpan RequestTimeout { get; private set; } protected TimeSpan RequestTimeout { get; private set; }
public RateLimitingBehaviour RateLimitBehaviour { get; private set; } public RateLimitingBehaviour RateLimitBehaviour { get; private set; }
public IEnumerable<IRateLimiter> RateLimiters { get; private set; } public IEnumerable<IRateLimiter> RateLimiters { get; private set; }
public int TotalRequestsMade { get; private set; }
protected RestClient(ClientOptions exchangeOptions, AuthenticationProvider authenticationProvider): base(exchangeOptions, authenticationProvider) protected RestClient(ClientOptions exchangeOptions, AuthenticationProvider authenticationProvider): base(exchangeOptions, authenticationProvider)
{ {
@ -270,6 +271,7 @@ namespace CryptoExchange.Net
try try
{ {
request.Timeout = RequestTimeout; request.Timeout = RequestTimeout;
TotalRequestsMade++;
var response = await request.GetResponse().ConfigureAwait(false); var response = await request.GetResponse().ConfigureAwait(false);
using (var reader = new StreamReader(response.GetResponseStream())) using (var reader = new StreamReader(response.GetResponseStream()))
{ {

View File

@ -35,6 +35,7 @@ namespace CryptoExchange.Net.Sockets
public bool ShouldReconnect { get; set; } public bool ShouldReconnect { get; set; }
public bool Reconnecting { get; set; } public bool Reconnecting { get; set; }
public string Origin { get; set; }
public string Url { get; } public string Url { get; }
public bool IsClosed => socket.State == WebSocketState.Closed; public bool IsClosed => socket.State == WebSocketState.Closed;
@ -173,6 +174,7 @@ namespace CryptoExchange.Net.Sockets
{ {
lock (socketLock) lock (socketLock)
{ {
log.Write(LogVerbosity.Debug, $"Socket {Id} resetting");
socket?.Dispose(); socket?.Dispose();
socket = null; socket = null;
} }
@ -187,7 +189,7 @@ namespace CryptoExchange.Net.Sockets
{ {
if (socket == null) if (socket == null)
{ {
socket = new WebSocket(Url, cookies: cookies.ToList(), customHeaderItems: headers.ToList()) socket = new WebSocket(Url, cookies: cookies.ToList(), customHeaderItems: headers.ToList(), origin: Origin ?? "")
{ {
EnableAutoSendPing = true, EnableAutoSendPing = true,
AutoSendPingInterval = 10 AutoSendPingInterval = 10
@ -243,7 +245,7 @@ namespace CryptoExchange.Net.Sockets
} }
if (socket.State == WebSocketState.Connecting) if (socket.State == WebSocketState.Connecting)
Close().Wait(); socket.Close();
return connected; return connected;
}).ConfigureAwait(false); }).ConfigureAwait(false);

View File

@ -1,4 +1,5 @@
using CryptoExchange.Net.Interfaces; using System.Collections.Generic;
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Logging; using CryptoExchange.Net.Logging;
namespace CryptoExchange.Net.Sockets namespace CryptoExchange.Net.Sockets
@ -9,5 +10,10 @@ namespace CryptoExchange.Net.Sockets
{ {
return new BaseSocket(log, url); return new BaseSocket(log, url);
} }
public IWebsocket CreateWebsocket(Log log, string url, IDictionary<string, string> cookies, IDictionary<string, string> headers)
{
return new BaseSocket(log, url, cookies, headers);
}
} }
} }