/// <summary> /// Get the <see cref="IRetryTaskController"/> associated with the /// <see cref="IBinanceWebSocketClient"/> web socket stream. /// </summary> /// <param name="manager"></param> /// <param name="client"></param> /// <returns></returns> public static IRetryTaskController GetController(this IBinanceWebSocketManager manager, IBinanceWebSocketClient client) { Throw.IfNull(manager, nameof(manager)); Throw.IfNull(client, nameof(client)); return(manager.GetController(client.WebSocket)); }
/// <summary> /// Begin all controller actions. /// </summary> /// <param name="manager"></param> public static void BeginAll(this IBinanceWebSocketManager manager) { Throw.IfNull(manager, nameof(manager)); foreach (var client in Clients(manager)) { GetController(manager, client).Begin(); } }
/// <summary> /// Cancel all controller actions. /// </summary> /// <param name="manager"></param> /// <returns></returns> public static async Task CancelAllAsync(this IBinanceWebSocketManager manager) { Throw.IfNull(manager, nameof(manager)); foreach (var client in Clients(manager)) { await GetController(manager, client) .CancelAsync() .ConfigureAwait(false); } }
/// <summary> /// Get all managed <see cref="IBinanceWebSocketClient"/> clients. /// </summary> /// <param name="manager"></param> /// <returns></returns> public static IEnumerable <IBinanceWebSocketClient> Clients(this IBinanceWebSocketManager manager) { Throw.IfNull(manager, nameof(manager)); yield return(manager.AggregateTradeClient); yield return(manager.CandlestickClient); yield return(manager.DepthClient); yield return(manager.StatisticsClient); yield return(manager.TradeClient); }
/// <summary> /// Cancel all controller actions and unsubscribe all streams from all clients. /// </summary> /// <param name="manager"></param> /// <returns></returns> public static async Task UnsubscribeAllAsync(this IBinanceWebSocketManager manager) { Throw.IfNull(manager, nameof(manager)); await CancelAllAsync(manager) .ConfigureAwait(false); var wasAutoStreamingDisabled = manager.IsAutoStreamingDisabled; manager.IsAutoStreamingDisabled = true; // disable auto-streaming. foreach (var client in Clients(manager)) { client.UnsubscribeAll(); // Wait for adapter asynchronous operation to complete. await((IBinanceWebSocketClientAdapter)client).Task .ConfigureAwait(false); } manager.IsAutoStreamingDisabled = wasAutoStreamingDisabled; // restore. }
public static async Task Main(string[] args) { // Un-comment to run... //await AccountBalancesExample.ExampleMain(args); //await MinimalWithDependencyInjection.ExampleMain(args); //await MinimalWithoutDependencyInjection.ExampleMain(args); //await SerializationExample.ExampleMain(args); //await OrderBookCacheAccountBalanceExample.ExampleMain(args); var cts = new CancellationTokenSource(); try { // Load configuration. Configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", true, false) .AddUserSecrets <Program>() // for access to API key and secret. .Build(); // Configure services. ServiceProvider = new ServiceCollection() .AddBinance() .AddKucoin() // Use a single web socket stream for combined streams (optional). .AddSingleton <IWebSocketStream, BinanceWebSocketStream>() // Change low-level web socket client implementation. //.AddTransient<IWebSocketClient, WebSocket4NetClient>() //.AddTransient<IWebSocketClient, WebSocketSharpClient>() .AddOptions() .AddLogging(builder => builder.SetMinimumLevel(LogLevel.Trace)) .Configure <BinanceApiOptions>(Configuration.GetSection("ApiOptions")) .Configure <KucoinApiOptions>(Configuration.GetSection("ApiOptions")) .Configure <Binance.UserDataWebSocketManagerOptions>(Configuration.GetSection("UserDataOptions")) .BuildServiceProvider(); // Configure logging. ServiceProvider .GetService <ILoggerFactory>() .AddConsole(Configuration.GetSection("Logging:Console")) .AddFile(Configuration.GetSection("Logging:File")); var apiKey = Configuration["BinanceApiKey"] // user secrets configuration. ?? Configuration.GetSection("BinanceUser")["ApiKey"]; // appsettings.json configuration. var apiSecret = Configuration["BinanceApiSecret"] // user secrets configuration. ?? Configuration.GetSection("BinanceUser")["ApiSecret"]; // appsettings.json configuration. if (string.IsNullOrWhiteSpace(apiKey) || string.IsNullOrWhiteSpace(apiSecret)) { PrintApiNotice(); } if (!string.IsNullOrEmpty(apiKey)) { User = ServiceProvider .GetService <IBinanceApiUserProvider>() .CreateUser(apiKey, apiSecret); } BinanceApi = ServiceProvider.GetService <IBinanceApi>(); KucoinApi = ServiceProvider.GetService <IKucoinApi>(); ClientManager = ServiceProvider.GetService <IBinanceWebSocketManager>(); ClientManager.Error += (s, e) => { lock (ConsoleSync) { Console.WriteLine(); Console.WriteLine($"! Client Manager Error: \"{e.Exception.Message}\""); Console.WriteLine(); } }; // Instantiate all assembly command handlers. foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) { if (typeof(IHandleCommand).IsAssignableFrom(type) && !type.IsAbstract) { CommandHandlers.Add((IHandleCommand)Activator.CreateInstance(type)); } } await SuperLoopAsync(cts.Token); } catch (Exception e) { lock (ConsoleSync) { Console.WriteLine($"! FAIL: \"{e.Message}\""); if (e.InnerException != null) { Console.WriteLine($" -> Exception: \"{e.InnerException.Message}\""); } } } finally { await DisableLiveTask(); cts.Cancel(); cts.Dispose(); User?.Dispose(); ClientManager?.Dispose(); lock (ConsoleSync) { Console.WriteLine(); Console.WriteLine(" ...press any key to close window."); Console.ReadKey(true); } } }