Example #1
0
        private async Task StartResilientStream(TimeSpan connectionTimeout)
        {
            if (_cancel.IsCancellationRequested)
            {
                return;
            }

            Current = ConnectionProvider.GetNextConnection(_onError, _onSuccess, Current == null ? null : Current.Endpoint);

            var isConnectionSet = await Current.Initialize();

            if (!isConnectionSet)
            {
                //if the connection failed to reach endpoint, we delay and try again
                await Task.Delay(connectionTimeout);
                await StartResilientStream(connectionTimeout);
            }
            else
            {
                Current.StatusStream.Subscribe(async current =>
                {
                    //if the connection abrutly closed, then we immediatly try to reach the next endpoint
                    if (current.ConnectionStatus == ConnectionStatus.Closed)
                    {
                        await StartResilientStream(connectionTimeout);
                    }
                });

                //if we conect to the current endpoint, then wire up the stream
                GetResilientStream(connectionTimeout);
            }
        }