Exemple #1
0
        public static async Task <bool> WaitForSend(CoinExSocketClient client, int timeout = 3000, string expectedData = null)
        {
            var evnt    = new ManualResetEvent(false);
            var handler = new Action <Mock <IWebsocket>, string>((s, data) =>
            {
                if (!client.sockets.Any())
                {
                    return;
                }

                var mock = Mock.Get(client.sockets[0].Socket);
                if (s.Equals(mock))
                {
                    if (expectedData == null || data == expectedData)
                    {
                        evnt.Set();
                    }
                }
            });

            OnSend += handler;
            return(await Task.Run(() =>
            {
                var result = evnt.WaitOne(timeout);
                OnSend -= handler;
                return result;
            }));
        }
 /// <summary>
 /// Create a new instance of CoinExSocketClient with default options
 /// </summary>
 public CoinExSocketClientSpotStreams(Log log, CoinExSocketClient baseClient, CoinExSocketClientOptions options)
     : base(options, options.SpotStreamsOptions)
 {
     _log        = log;
     _options    = options;
     _baseClient = baseClient;
 }
Exemple #3
0
        public static async Task <bool> WaitForClose(CoinExSocketClient client, int timeout = 1000)
        {
            var evnt    = new ManualResetEvent(false);
            var handler = new Action <Mock <IWebsocket> >((s) =>
            {
                if (!client.sockets.Any())
                {
                    return;
                }

                var mock = Mock.Get(client.sockets[0].Socket);
                if (s.Equals(mock))
                {
                    evnt.Set();
                }
            });

            OnClose += handler;
            return(await Task.Run(() =>
            {
                var result = evnt.WaitOne(timeout);
                OnClose -= handler;
                return result;
            }));
        }
Exemple #4
0
        public static void InvokeWebsocket(CoinExSocketClient client, string data)
        {
            var sockets = GetSockets(client);
            var mock    = Mock.Get(sockets[0].Socket);

            mock.Raise(r => r.OnMessage += null, data);
        }
Exemple #5
0
        public static void CloseWebsocket(CoinExSocketClient client)
        {
            var mock = Mock.Get(client.sockets[0].Socket);

            mock.Setup(s => s.IsOpen).Returns(() => false);
            mock.Setup(s => s.IsClosed).Returns(() => true);

            mock.Raise(r => r.OnClose += null);
        }
Exemple #6
0
 private void InvokeSubUpdate(CoinExSocketClient client, string method, params object[] parameters)
 {
     TestHelpers.InvokeWebsocket(client, JsonConvert.SerializeObject(new CoinExSocketResponse()
     {
         Id         = null,
         Method     = method,
         Parameters = parameters
     },
                                                                     new TimestampSecondsConverter()));
 }
Exemple #7
0
        public override void SetupConnector()
        {
            this.Client = new CoinExClient(new CoinExClientOptions()
            {
                ApiCredentials = new CryptoExchange.Net.Authentication.ApiCredentials(this.InParameters["apiKey"].GetValue().ToString(), this.InParameters["apiSecret"].GetValue().ToString())
            });

            this.SocketClient = new CoinExSocketClient();
            this.Next();
        }
Exemple #8
0
 private void InvokeSubResponse(CoinExSocketClient client, bool previousId = false)
 {
     TestHelpers.InvokeWebsocket(client, JsonConvert.SerializeObject(
                                     new CoinExSocketRequestResponse <CoinExSocketRequestResponseMessage>()
     {
         Error  = null,
         Id     = previousId ? CoinExSocketClient.LastId - 1: CoinExSocketClient.LastId,
         Result = new CoinExSocketRequestResponseMessage()
         {
             Status = "success"
         }
     }));
 }
Exemple #9
0
        static async Task Main(string[] args)
        {
            string[] marketList = new string[0];
            using (var client = new CoinExClient())
            {
                var listResult = await client.SpotApi.ExchangeData.GetSymbolsAsync();

                if (!listResult.Success)
                {
                    Console.WriteLine("Failed to get market list: " + listResult.Error);
                }
                else
                {
                    Console.WriteLine("Supported market list: " + string.Join(", ", listResult.Data));
                    marketList = listResult.Data.ToArray();
                }
            }

            Console.WriteLine();
            string market = null;

            while (true)
            {
                Console.WriteLine("Enter market name to subscribe to state updates");
                market = Console.ReadLine();
                if (!marketList.Contains(market.ToUpper()))
                {
                    Console.WriteLine("Unknown market, try again");
                }
                else
                {
                    break;
                }
            }

            var socketClient = new CoinExSocketClient(new Objects.CoinExSocketClientOptions()
            {
                LogLevel   = LogLevel.Information,
                LogWriters = new List <ILogger> {
                    new ConsoleLogger()
                }
            });
            await socketClient.SpotStreams.SubscribeToTickerUpdatesAsync(market, data =>
            {
                Console.WriteLine($"Last price of {market}: {data.Data.Close}");
            });

            Console.ReadLine();
        }
Exemple #10
0
        static void Main(string[] args)
        {
            string[] marketList = new string[0];
            using (var client = new CoinExClient())
            {
                var listResult = client.GetMarketList();
                if (!listResult.Success)
                {
                    Console.WriteLine("Failed to get market list: " + listResult.Error);
                }
                else
                {
                    Console.WriteLine("Support market list: " + string.Join(", ", listResult.Data));
                    marketList = listResult.Data;
                }
            }

            Console.WriteLine();
            string market = null;

            while (true)
            {
                Console.WriteLine("Enter market name to subscribe to state updates");
                market = Console.ReadLine();
                if (!marketList.Contains(market.ToUpper()))
                {
                    Console.WriteLine("Unknown market, try again");
                }
                else
                {
                    break;
                }
            }

            var socketClient = new CoinExSocketClient(new Objects.CoinExSocketClientOptions()
            {
                LogVerbosity = LogVerbosity.Info,
                LogWriters   = new List <TextWriter> {
                    Console.Out
                }
            });

            socketClient.SubscribeToMarketStateUpdates(market, (marketName, data) =>
            {
                Console.WriteLine($"Last price of {market}: {data.Close}");
            });
            Console.ReadLine();
        }
Exemple #11
0
        public static async Task <bool> WaitForConnect(CoinExSocketClient client, int timeout = 1000)
        {
            var evnt    = new ManualResetEvent(false);
            var handler = new Action <Mock <IWebsocket> >((s) =>
            {
                evnt.Set();
            });

            OnOpen += handler;
            return(await Task.Run(() =>
            {
                var result = evnt.WaitOne(timeout);
                OnOpen -= handler;
                return result;
            }));
        }
Exemple #12
0
 public static List <SocketConnection> GetSockets(CoinExSocketClient client)
 {
     return(((ConcurrentDictionary <int, SocketConnection>)client.GetType().GetField("sockets", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(client)).Values.ToList());
 }
Exemple #13
0
 public static List <SocketSubscription> GetSockets(CoinExSocketClient client)
 {
     return((List <SocketSubscription>)client.GetType().GetField("sockets", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(client));
 }