Ejemplo n.º 1
0
        public async Task Connect()
        {
            if (!_client.IsSignedIn)
            {
                throw new Exception("Unauthorized");
            }

            if (_socket != null && _socket.State != WebSocketState.Closed)
            {
                throw new Exception("Busy");
            }

            _prince = await _client.GetPrincipals(TDPrincipalsFields.streamerConnectionInfo, TDPrincipalsFields.streamerSubscriptionKeys, TDPrincipalsFields.preferences);

            _account = _prince.accounts.Find(o => o.accountId == _prince.primaryAccountId);

            if (_socket != null)
            {
                _socket.Dispose();
                _socket = null;
            }

            try
            {
                Console.WriteLine($"TDAmeritradeStreamClient Connect");

                _firstMessage = false;
                var path = new Uri("wss://" + _prince.streamerInfo.streamerSocketUrl + "/ws");
                _socket = new ClientWebSocket();

                await _socket.ConnectAsync(path, _cancel.Token);

                if (_socket.State == WebSocketState.Open)
                {
                    await Login();

                    Receive();
                }

                await TaskEx.WaitUntil(() => _firstMessage, timeout : 15);

                if (!_firstMessage)
                {
                    throw new Exception("Timeout");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"ERROR - {ex.Message}");

                if (_socket != null)
                {
                    _socket.Dispose();
                    _socket = null;
                }
            }
        }
        /// <summary>
        /// Connects to the live stream service
        /// </summary>
        /// <returns></returns>
        public async Task Connect()
        {
            try
            {
                if (!_client.IsSignedIn)
                {
                    throw new Exception("Unauthorized");
                }

                if (_socket != null && _socket.State != WebSocketState.Closed)
                {
                    throw new Exception("Busy");
                }


                _prince = await _client.GetPrincipals(TDPrincipalsFields.streamerConnectionInfo, TDPrincipalsFields.streamerSubscriptionKeys, TDPrincipalsFields.preferences);

                _account = _prince.accounts.Find(o => o.accountId == _prince.primaryAccountId);

                var path = new Uri("wss://" + _prince.streamerInfo.streamerSocketUrl + "/ws");
                _socket = new ClientWebSocket();

                await _socket.ConnectAsync(path, CancellationToken.None);

                if (_socket.State == WebSocketState.Open)
                {
                    await Login();

                    Receive();
                    IsConnected = true;
                }
            }
            catch (Exception ex)
            {
                OnException(ex);
                Cleanup();
            }
        }