Skip to content

Commit

Permalink
Add auto reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
halter73 committed Sep 20, 2019
1 parent 760bfc0 commit 71e4cdb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
15 changes: 12 additions & 3 deletions SignalR30SensorClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging;

namespace SignalR30SensorClient
{
Expand All @@ -12,16 +13,24 @@ class Program
static async Task Main(string[] args)
{
var hubConnectionBuilder = new HubConnectionBuilder()
.WithUrl("https://localhost:5001/sensors");
.WithUrl("https://localhost:5001/sensors")
.WithAutomaticReconnect()
.ConfigureLogging(logBuilder =>
{
logBuilder.AddConsole();
});

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.StartAsync();

Console.WriteLine("Started sensor {0}.", args[0]);

var cancellationTokenSource = new CancellationTokenSource();

await hubConnection.SendAsync("PublishSensorData", args[0], GenerateSensorData(cancellationTokenSource.Token));

Console.ReadLine();
Expand Down
3 changes: 3 additions & 0 deletions SignalR30SensorClient/SignalR30SensorClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client">
<Version>3.0.0-rc1.19457.4</Version>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Console">
<Version>3.0.0-rc1.19457.4</Version>
</PackageReference>
</ItemGroup>

</Project>
19 changes: 15 additions & 4 deletions SignalR30SensorWebApplication/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ <h1>SignalR 3.0 Sensor Demo</h1>
<script src="lib/signalr/signalr.js"></script>
<script src="lib/realTimeLineChart.js"></script>
<script>
const sensorNames = [];
const lineArray = [];
const latestSensorData = { x: -18, y: -18, z: -18};
const chart = realTimeLineChart();
Expand Down Expand Up @@ -60,12 +61,20 @@ <h1>SignalR 3.0 Sensor Demo</h1>
(async () => {
const connection = new signalR.HubConnectionBuilder()
.withUrl("/sensors")
.withAutomaticReconnect()
.configureLogging(signalR.LogLevel.Information)
.build();

let sensorNames = [];

connection.onreconnected(async connectionId => {
sensorNames = await connection.invoke("GetSensorNames");
sensorNames.forEach(addSensor);
});

await connection.start();

const sensorNames = await connection.invoke("GetSensorNames");
sensorNames = await connection.invoke("GetSensorNames");

function removeSensor(sensor) {
const index = sensorNames.indexOf(sensor);
Expand All @@ -92,13 +101,15 @@ <h1>SignalR 3.0 Sensor Demo</h1>
});
}

sensorNames.forEach(addSensor);
connection.on("SensorAdded", sensorName => {
function tryAddSensor(sensorName) {
if (!sensorNames.includes(sensorName)) {
sensorNames.push(sensorName);
addSensor(sensorName);
}
});
}

sensorNames.forEach(addSensor);
connection.on("SensorAdded", tryAddSensor);

function resize() {
if (d3.select("#chart svg").empty()) {
Expand Down

0 comments on commit 71e4cdb

Please sign in to comment.