コード例 #1
0
        static async Task Main(string[] args)
        {
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

            using var channel = GrpcChannel.ForAddress("http://localhost:5000");

            var client = new StockMarketServiceClient(channel);

            var cts = new CancellationTokenSource();

            cts.CancelAfter(TimeSpan.FromSeconds(5));


            using var replies = client.GetStockMarketStream(new StockStreamRequest(), cancellationToken: cts.Token);

            try
            {
                await foreach (var stockData in replies.ResponseStream.ReadAllAsync(cancellationToken: cts.Token))
                {
                    PrintStockInfo(stockData);
                }
            }
            catch
            {
                replies.Dispose();
                Console.WriteLine("Stream cancelled.");
            }


            Console.WriteLine("Press a key to exit");

            Console.ReadKey();
        }
コード例 #2
0
        public HomePage()
        {
            InitializeComponent();

            chartPicker.SelectedIndexChanged += chartPicker_SelectedIndexChanged;
            StartButton.Clicked      += StartButton_Clicked;
            StopButton.Clicked       += StopButton_Clicked;
            ShowSplineSwitch.Toggled += ShowSplineSwitch_Toggled;
            _client = new StockMarketServiceClient();

            chart.ItemSource = _data = new StockMarketDataSample();

            foreach (var chartType in Enum.GetValues(typeof(PriceDisplayType)))
            {
                chartPicker.Items.Add(chartType.ToString());
            }
            chartPicker.SelectedIndex = 0;
        }
コード例 #3
0
 public StockMarketStreamHub(StockMarketServiceClient client, IDispatchMessage <Stock> dispatchMessage)
 {
     _client = client;
     this._dispatchMessage = dispatchMessage;
 }
コード例 #4
0
        static async Task Main(string[] args)
        {
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

            using var channel = GrpcChannel.ForAddress("http://localhost:5000");
            var client = new StockMarketServiceClient(channel);

            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));

            using var stockStreamService = client.GetStockStream();

            var responseProcessing = Task.Run(async() =>
            {
                try
                {
                    await foreach (var stockReply in stockStreamService.ResponseStream.ReadAllAsync())
                    {
                        PrintStockInfo(stockReply);
                    }
                }
                catch (RpcException ex) when(ex.StatusCode == StatusCode.Cancelled)
                {
                    Console.WriteLine("Stream cancelled.");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error reading response: " + ex);
                }
            });

            var datesUtc = Dates.Select(datesUtc => datesUtc.ToUniversalTime()).ToArray();

            for (int i = 0; i < 5; i++)
            {
                foreach (var symbol in new[] { "AAPL", "AMZN", "FB", "GOOG", "MSFT" })
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine($"Requesting stock info for {symbol}...");
                    Console.ResetColor();

                    int index = rnd.Next(Dates.Count);
                    var date  = datesUtc[index].ToTimestamp();
                    Console.WriteLine(
                        $"Requesting Stock {symbol} history for date {date.ToDateTime().ToString("MM/dd/yyyy")}...");

                    await stockStreamService.RequestStream.WriteAsync(new StockRequest
                    {
                        Symbol = symbol,
                        Date   = date
                    });

                    await Task.Delay(1500); // simulate delay getting next item
                }
            }

            Console.WriteLine("Completing request stream");

            await stockStreamService.RequestStream.CompleteAsync();

            Console.WriteLine("Request stream completed");

            await responseProcessing;

            Console.WriteLine("Read all responses");
            Console.WriteLine("Press a key to exit");
            Console.ReadKey();
        }