示例#1
0
        public async Task SubscribeAccountInfo(Interface.Model.User user, Action <AccountInfoEventArgs> callback, Action <Exception> exception, CancellationToken cancellationToken)
        {
            var apiUser       = new BinanceApiUser(user.ApiKey, user.ApiSecret);
            var streamControl = new UserDataWebSocketStreamControl(binanceApi);
            var listenKey     = await streamControl.OpenStreamAsync(apiUser).ConfigureAwait(false);

            var accountInfoCache = new AccountInfoCache(binanceApi, new UserDataWebSocketClient());

            accountInfoCache.Subscribe(listenKey, apiUser, e =>
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    accountInfoCache.Unsubscribe();
                    return;
                }

                try
                {
                    var accountInfo  = GetAccountInfo(e.AccountInfo);
                    accountInfo.User = user;
                    callback.Invoke(new AccountInfoEventArgs {
                        AccountInfo = accountInfo
                    });
                }
                catch (Exception ex)
                {
                    accountInfoCache.Unsubscribe();
                    exception.Invoke(ex);
                    return;
                }
            });
        }
        public void Unsubscribe()
        {
            var api    = new Mock <IBinanceApi>().Object;
            var client = new Mock <IUserDataClient>().Object;

            var cache = new AccountInfoCache(api, client);

            // Can call unsubscribe before subscribe or multiple times without fail.
            cache.Unsubscribe();
            cache.Unsubscribe();
        }