public async Task WhenClientSendsTextMessageThenResponds()
            {
                const string Message                    = "Message";
                var          waitHandle                 = new ManualResetEventSlim(false);
                var          clientSslConfiguration     = new ClientSslConfiguration("localhost", certificateValidationCallback: (sender, certificate, chain, errors) => true);
                Func <MessageEventArgs, Task> onMessage = e =>
                {
                    if (e.Text.ReadToEnd() == Message)
                    {
                        waitHandle.Set();
                    }
                    return(AsyncEx.Completed());
                };

                using (var client = new WebSocket("wss://localhost:443/echo", sslAuthConfiguration: clientSslConfiguration, onMessage: onMessage))
                {
                    await client.Connect().ConfigureAwait(false);

                    await client.Send(Message).ConfigureAwait(false);

                    var result = waitHandle.Wait(Debugger.IsAttached ? 30000 : 2000);

                    Assert.True(result);
                }
            }
Beispiel #2
0
                                                        stockData)));         //#A

        async Task <Tuple <string, StockData[]> > ProcessStockHistoryRetry(string symbol)
        {
            string stockHistory =
                await AsyncEx.Retry(() => DownloadStockHistory(symbol), 5, TimeSpan.FromSeconds(2));

            StockData[] stockData = await ConvertStockHistory(stockHistory);

            return(Tuple.Create(symbol, stockData));
        }
Beispiel #3
0
        async Task <Tuple <string, StockData[]> > ProcessStockHistoryComplete(string symbol)
        {
            var stockData = await AsyncEx.Retry(() =>
                                                DownloadStockHistory(symbol)
                                                .OrElse(() => StreamReadStockHistory(symbol))
                                                .Bind(stockHistory => ConvertStockHistory(stockHistory)), 3, TimeSpan.FromSeconds(1));


            return(Tuple.Create(symbol, stockData));
        }
        /// <summary>
        /// Closes the session with the specified <paramref name="id"/>, <paramref name="code"/>,
        /// and <paramref name="reason"/>.
        /// </summary>
        /// <param name="id">
        /// A <see cref="string"/> that represents the ID of the session to close.
        /// </param>
        /// <param name="code">
        /// One of the <see cref="CloseStatusCode"/> enum values, represents the status code
        /// indicating the reason for the close.
        /// </param>
        /// <param name="reason">
        /// A <see cref="string"/> that represents the reason for the close.
        /// </param>
        public Task CloseSession(string id, CloseStatusCode code, string reason)
        {
            IWebSocketSession session;

            if (TryGetSession(id, out session))
            {
                return(session.Context.WebSocket.Close(code, reason));
            }
            return(AsyncEx.Completed());
        }
Beispiel #5
0
        static async Task RunDownloadImageWithRetry()
        {
            // DEMO 6.2
            // Combinator Retry/otherwise
            Image image = await AsyncEx.Retry(async() =>
                                              await DownloadImageAsync("Bugghina001.jpg")
                                              .Otherwise(async() =>
                                                         await DownloadImageAsync("Bugghina002.jpg")),
                                              5, TimeSpan.FromSeconds(2));

            ProcessImage(image);
        }
Beispiel #6
0
        async Task <Tuple <string, StockData[]> > ProcessStockHistoryConditional(string symbol)
        {
            Func <Func <string, string>, Func <string, Task <string> > > downloadStock =
                service => stock => DownloadStockHistory(service, stock);  // #C

            Func <string, Task <string> > googleService =
                downloadStock(googleSourceUrl);                                           // #D
            Func <string, Task <string> > yahooService =
                downloadStock(yahooSourceUrl);                                            // #D

            return(await AsyncEx.Retry(                                                   // #E
                       () => googleService(symbol).Otherwise(() => yahooService(symbol)), //#F
                       5, TimeSpan.FromSeconds(2))
                   .Bind(data => ConvertStockHistory(data))                               // #G
                   .Map(prices => Tuple.Create(symbol, prices)));                         // #H
        }
        private async Task <IEnumerable <AlbumTracksSearchEntry> > GetAllMusicTracksInternal(string searchString,
                                                                                             CancellationToken token)
        {
            _logger.LogInformation(searchString);

            var client =
                new RestClient($"https://itunes.apple.com/lookup?id={searchString}&entity=song");
            var request = new RestRequest(Method.GET);

            IRestResponse response = await AsyncEx.Retry(() => client.ExecuteAsync(request, token), 3,
                                                         TimeSpan.FromMilliseconds(100), token);

            var searchResponse =
                JsonConvert.DeserializeObject <ApiResponse <AlbumTracksSearchEntry> >(response.Content);

            await _cachingProvider.SetAsync(searchString, searchResponse.Results,
                                            TimeSpan.FromMinutes(ExpirationCacheInMinutes));

            return(searchResponse.Results.Where(x => x.TrackId != null));
        }
Beispiel #8
0
 private Task PrintError(ErrorEventArgs e)
 {
     Console.WriteLine(e.Message);
     return(AsyncEx.Completed());
 }
Beispiel #9
0
 static void Main(string[] args)
 {
     //Код не менял, т.к. он итак может вывести больше одной цепочки символов в столбце >>>> MatrixStart.Start();
     Console.ReadKey();
     var i = AsyncEx.Start();
 }
        /// <summary>
        /// Closes the session with the specified <paramref name="id"/>.
        /// </summary>
        /// <param name="id">
        /// A <see cref="string"/> that represents the ID of the session to close.
        /// </param>
        public Task CloseSession(string id)
        {
            IWebSocketSession session;

            return(TryGetSession(id, out session) ? session.Context.WebSocket.Close() : AsyncEx.Completed());
        }