-
Notifications
You must be signed in to change notification settings - Fork 5
/
scratch.txt
175 lines (140 loc) · 3.94 KB
/
scratch.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
public class SensorHub : Hub
{
private readonly SensorCollection _sensorCollection;
public SensorHub(SensorCollection sensorCollection)
{
_sensorCollection = sensorCollection;
}
public IEnumerable<string> GetSensorNames()
{
return _sensorCollection.GetSensorNames();
}
public IAsyncEnumerable<double> GetSensorData(string sensorName, CancellationToken cancellationToken)
{
return _sensorCollection.GetSensorData(sensorName, cancellationToken);
}
public async Task PublishSensorData(string sensorName, IAsyncEnumerable<double> sensorData)
{
try
{
await foreach (var measurement in sensorData)
{
_sensorCollection.PublishSensorData(sensorName, measurement);
}
}
finally
{
_sensorCollection.DisconnectSensor(sensorName);
}
}
}
class Program
{
static async Task Main(string[] args)
{
var hubConnectionBuilder = new HubConnectionBuilder()
.WithUrl("https://localhost:5001/sensors");
await using var hubConnection = hubConnectionBuilder.Build();
await hubConnection.StartAsync();
await hubConnection.SendAsync("PublishSensorData", args[0], GenerateSensorData());
Console.ReadLine();
}
static async IAsyncEnumerable<double> GenerateSensorData()
{
var rng = new Random();
while (true)
{
yield return rng.NextDouble() * 10;
await Task.Delay(1000);
}
}
}
<script>
(async () => {
const latestSensorData = { x: 0, y: 0, z: 0 };
const connection = new signalR.HubConnectionBuilder()
.withUrl("/sensors")
.configureLogging(signalR.LogLevel.Information)
.build();
function subscribeToSensor(sensorName) {
connection.stream("GetSensorData", sensorName)
.subscribe({
next: (item) => {
console.log(`${sensorName}: ${item}`);
latestSensorData[sensorName] = item;
},
complete: () => {
console.log(`${sensorName} Completed`);
},
error: (err) => {
console.log(`${sensorName} error: "${err}"`);
},
});
}
await connection.start();
const sensorNames = await connection.invoke("GetSensorNames");
sensorNames.forEach(subscribeToSensor);
connection.on("SensorAdded", subscribeToSensor);
startRealTimeLineChart(latestSensorData);
})();
</script>
# With Auto Reconnect
class Program
{
static async Task Main(string[] args)
{
var hubConnectionBuilder = new HubConnectionBuilder()
.WithUrl("https://localhost:5001/sensors")
.WithAutomaticReconnect();
await using var hubConnection = hubConnectionBuilder.Build();
hubConnection.Reconnected += async connectionId => {
await hubConnection.SendAsync("PublishSensorData", args[0], GenerateSensorData());
};
await hubConnection.StartAsync();
await hubConnection.SendAsync("PublishSensorData", args[0], GenerateSensorData());
Console.ReadLine();
}
static async IAsyncEnumerable<double> GenerateSensorData()
{
var rng = new Random();
while (true)
{
yield return rng.NextDouble() * 10;
await Task.Delay(1000);
}
}
}
<script>
(async () => {
const latestSensorData = { x: 0, y: 0, z: 0 };
const connection = new signalR.HubConnectionBuilder()
.withUrl("/sensors")
.withAutomaticReconnect()
.configureLogging(signalR.LogLevel.Information)
.build();
function subscribeToSensor(sensorName) {
connection.stream("GetSensorData", sensorName)
.subscribe({
next: (item) => {
console.log(`${sensorName}: ${item}`);
latestSensorData[sensorName] = item;
},
complete: () => {
console.log(`${sensorName} Completed`);
},
error: (err) => {
console.log(`${sensorName} error: "${err}"`);
},
});
}
connection.onreconnected(async connectionId => {
const sensorNames = await connection.invoke("GetSensorNames");
sensorNames.forEach(subscribeToSensor);
});
await connection.start();
const sensorNames = await connection.invoke("GetSensorNames");
sensorNames.forEach(subscribeToSensor);
connection.on("SensorAdded", subscribeToSensor);
startRealTimeLineChart(latestSensorData);
})();
</script>