From 989b1b35ea6fe725e2b4cc156cc0abac8b018c2b Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Fri, 20 Sep 2019 11:41:50 -0700 Subject: [PATCH] Simplify demo --- SignalR30SensorClient/Program.cs | 45 ++------- .../SignalR30SensorClient.csproj | 3 - SignalR30SensorWebApplication/SensorHub.cs | 5 +- .../wwwroot/index.html | 93 ++++--------------- .../wwwroot/lib/realTimeLineChart.js | 30 ++++++ global.json | 2 +- 6 files changed, 55 insertions(+), 123 deletions(-) diff --git a/SignalR30SensorClient/Program.cs b/SignalR30SensorClient/Program.cs index 99cbf6d..dabcbc1 100644 --- a/SignalR30SensorClient/Program.cs +++ b/SignalR30SensorClient/Program.cs @@ -1,10 +1,7 @@ using System; using System.Collections.Generic; -using System.Runtime.CompilerServices; -using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.SignalR.Client; -using Microsoft.Extensions.Logging; namespace SignalR30SensorClient { @@ -14,57 +11,29 @@ static async Task Main(string[] args) { var hubConnectionBuilder = new HubConnectionBuilder() .WithUrl("https://localhost:5001/sensors") - .WithAutomaticReconnect() - .ConfigureLogging(logBuilder => - { - logBuilder.AddConsole(); - }); + .WithAutomaticReconnect(); await using var hubConnection = hubConnectionBuilder.Build(); - var cancellationTokenSource = new CancellationTokenSource(); hubConnection.Reconnected += async connectionId => { - await hubConnection.SendAsync("PublishSensorData", args[0], GenerateSensorData(cancellationTokenSource.Token)); + await hubConnection.SendAsync("PublishSensorData", args[0], GenerateSensorData()); }; await hubConnection.StartAsync(); - Console.WriteLine("Started sensor {0}.", args[0]); - - await hubConnection.SendAsync("PublishSensorData", args[0], GenerateSensorData(cancellationTokenSource.Token)); - - Console.ReadLine(); - - cancellationTokenSource.Cancel(); + await hubConnection.SendAsync("PublishSensorData", args[0], GenerateSensorData()); Console.ReadLine(); } - static async IAsyncEnumerable GenerateSensorData([EnumeratorCancellation] CancellationToken cancellationToken) + static async IAsyncEnumerable GenerateSensorData() { var rng = new Random(); - double currentTemp = -18; - double tempVelocity = 0.5; - while (!cancellationToken.IsCancellationRequested) + while (true) { - tempVelocity += (rng.NextDouble() - .5) / 4; - tempVelocity = Math.Clamp(tempVelocity, -2, 2); - - currentTemp += tempVelocity; - - if (currentTemp < -30) - { - tempVelocity = .5; - } - else if(currentTemp > 0) - { - tempVelocity = -.5; - } - - yield return currentTemp; - - await Task.Delay(1000, cancellationToken); + yield return rng.NextDouble() * 10; + await Task.Delay(1000); } } } diff --git a/SignalR30SensorClient/SignalR30SensorClient.csproj b/SignalR30SensorClient/SignalR30SensorClient.csproj index 8c7f750..6091cd6 100644 --- a/SignalR30SensorClient/SignalR30SensorClient.csproj +++ b/SignalR30SensorClient/SignalR30SensorClient.csproj @@ -9,9 +9,6 @@ 3.0.0-rc1.19457.4 - - 3.0.0-rc1.19457.4 - diff --git a/SignalR30SensorWebApplication/SensorHub.cs b/SignalR30SensorWebApplication/SensorHub.cs index 44d93cf..f05afff 100644 --- a/SignalR30SensorWebApplication/SensorHub.cs +++ b/SignalR30SensorWebApplication/SensorHub.cs @@ -10,12 +10,10 @@ namespace SignalR30SensorWebApplication public class SensorHub : Hub { private readonly SensorCollection _sensorCollection; - private readonly ILogger _logger; - public SensorHub(SensorCollection sensorCollection, ILogger logger) + public SensorHub(SensorCollection sensorCollection) { _sensorCollection = sensorCollection; - _logger = logger; } public IEnumerable GetSensorNames() @@ -34,7 +32,6 @@ public async Task PublishSensorData(string sensorName, IAsyncEnumerable { await foreach (var measurement in sensorData) { - _logger.LogDebug("Received sensor data from {sensorName}: {measurement}", sensorName, measurement); _sensorCollection.PublishSensorData(sensorName, measurement); } } diff --git a/SignalR30SensorWebApplication/wwwroot/index.html b/SignalR30SensorWebApplication/wwwroot/index.html index 1d8a5ed..d378236 100644 --- a/SignalR30SensorWebApplication/wwwroot/index.html +++ b/SignalR30SensorWebApplication/wwwroot/index.html @@ -12,7 +12,6 @@

SignalR 3.0 Sensor Demo


-