From 9e8ccd6a0f32bb5e4c78b892aea48c8f15856588 Mon Sep 17 00:00:00 2001 From: JKorf Date: Fri, 7 Sep 2018 12:12:56 +0200 Subject: [PATCH] Added state getter for socket --- .../Implementation/BaseSocket.cs | 10 +++ .../Implementation/TestWebsocket.cs | 86 ------------------- .../Implementation/TestWebsocketFactory.cs | 13 --- CryptoExchange.Net/Interfaces/IWebsocket.cs | 2 + 4 files changed, 12 insertions(+), 99 deletions(-) delete mode 100644 CryptoExchange.Net/Implementation/TestWebsocket.cs delete mode 100644 CryptoExchange.Net/Implementation/TestWebsocketFactory.cs diff --git a/CryptoExchange.Net/Implementation/BaseSocket.cs b/CryptoExchange.Net/Implementation/BaseSocket.cs index d5bd2dc..bbb18a8 100644 --- a/CryptoExchange.Net/Implementation/BaseSocket.cs +++ b/CryptoExchange.Net/Implementation/BaseSocket.cs @@ -39,6 +39,16 @@ namespace CryptoExchange.Net.Implementation set => socket.AutoSendPingInterval = (int) Math.Round(value.TotalSeconds); } + public WebSocketState SocketState + { + get + { + if (socket == null) + return WebSocketState.None; + return socket.State; + } + } + public BaseSocket(Log log, string url):this(log, url, new Dictionary(), new Dictionary()) { } diff --git a/CryptoExchange.Net/Implementation/TestWebsocket.cs b/CryptoExchange.Net/Implementation/TestWebsocket.cs deleted file mode 100644 index 843abed..0000000 --- a/CryptoExchange.Net/Implementation/TestWebsocket.cs +++ /dev/null @@ -1,86 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Security.Authentication; -using System.Threading.Tasks; -using CryptoExchange.Net.Interfaces; - -namespace CryptoExchange.Net.Implementation -{ - public class TestWebsocket: IWebsocket - { - public List MessagesSend = new List(); - - public void Dispose() - { - } - - public void SetEnabledSslProtocols(SslProtocols protocols) - { - } - - public void SetProxy(string host, int port) - { - } - - public event Action OnClose; - public event Action OnMessage; - public event Action OnError; - public event Action OnOpen; - - public bool IsClosed { get; private set; } = true; - public bool IsOpen { get; private set; } - public bool PingConnection { get; set; } - public TimeSpan PingInterval { get; set; } - - public bool HasConnection = true; - - public Task Connect() - { - if (!HasConnection) - { - OnError?.Invoke(new Exception("No connection")); - return Task.FromResult(false); - } - - IsClosed = false; - IsOpen = true; - OnOpen?.Invoke(); - - return Task.FromResult(true); - } - - public void Send(string data) - { - if (!HasConnection) - { - OnError?.Invoke(new Exception("No connection")); - Close(); - return; - } - - MessagesSend.Add(data); - } - - public async Task EnqueueMessage(string data, int wait) - { - await Task.Delay(wait); - OnMessage?.Invoke(data); - } - - public async Task InvokeError(Exception ex, bool closeConnection) - { - await Task.Delay(10); - OnError?.Invoke(ex); - if (closeConnection) - await Close(); - } - - public Task Close() - { - IsClosed = true; - IsOpen = false; - OnClose?.Invoke(); - return Task.FromResult(0); - } - } -} diff --git a/CryptoExchange.Net/Implementation/TestWebsocketFactory.cs b/CryptoExchange.Net/Implementation/TestWebsocketFactory.cs deleted file mode 100644 index d630e84..0000000 --- a/CryptoExchange.Net/Implementation/TestWebsocketFactory.cs +++ /dev/null @@ -1,13 +0,0 @@ -using CryptoExchange.Net.Interfaces; -using CryptoExchange.Net.Logging; - -namespace CryptoExchange.Net.Implementation -{ - public class TestWebsocketFactory : IWebsocketFactory - { - public IWebsocket CreateWebsocket(Log log, string url) - { - return new TestWebsocket(); - } - } -} diff --git a/CryptoExchange.Net/Interfaces/IWebsocket.cs b/CryptoExchange.Net/Interfaces/IWebsocket.cs index fe4518a..b6bdd54 100644 --- a/CryptoExchange.Net/Interfaces/IWebsocket.cs +++ b/CryptoExchange.Net/Interfaces/IWebsocket.cs @@ -1,6 +1,7 @@ using System; using System.Security.Authentication; using System.Threading.Tasks; +using WebSocket4Net; namespace CryptoExchange.Net.Interfaces { @@ -13,6 +14,7 @@ namespace CryptoExchange.Net.Interfaces event Action OnError; event Action OnOpen; + WebSocketState SocketState { get; } bool IsClosed { get; } bool IsOpen { get; } bool PingConnection { get; set; }