1
+ @page " /plot"
2
+
3
+ @using BlazorSignalRApp .Data ;
4
+ @using Microsoft .AspNetCore .SignalR .Client
5
+ @using System .Diagnostics ;
6
+ @inject NavigationManager NavManager
7
+ @implements IAsyncDisposable
8
+
9
+ <div >
10
+ @* Plot component*@
11
+ <RadzenChart >
12
+ <RadzenLineSeries Data =" @Samples" CategoryProperty =" Quarter" ValueProperty =" Revenue" />
13
+ </RadzenChart >
14
+
15
+ <div class =" form-group" >
16
+ <label >
17
+ Samples Per Time Interval : <input @bind =" SamplePerTI" />
18
+ </label >
19
+ </div >
20
+ <div class =" form-group" >
21
+ <label >
22
+ Time Interval in MS : <input @bind =" TimeIntervalInMS" />
23
+ </label >
24
+ </div >
25
+ <button @onclick =" Send" disabled =" @(!IsConnected)" >Send</button >
26
+ <button @onclick =" ResetPlotStream" disabled =" @(!IsConnected)" >Reset</button >
27
+ </div >
28
+
29
+ @code {
30
+ private HubConnection ? hubConnection ;
31
+ private int SamplePerTI ;
32
+ private int TimeIntervalInMS ;
33
+ public int prevSampleCount = 0 ;
34
+ PlotDataItem [] Samples { get ; set ; }
35
+ List <PlotDataItem > SamplesList = new ();
36
+
37
+ protected override async Task OnInitializedAsync ()
38
+ {
39
+ // Create a signalR client for the required hub
40
+ hubConnection = new HubConnectionBuilder ()
41
+ .WithUrl (NavManager .ToAbsoluteUri (" /plothub" ))
42
+ .WithAutomaticReconnect ()
43
+ .Build ();
44
+
45
+ // Subscribe and register call back for the methods that you want to listen to
46
+ hubConnection .On <PlotDataItem []>(" ReceiveSamples" , (samples ) => InitializeDataReception (samples )
47
+
48
+ );
49
+
50
+ await hubConnection .StartAsync ();
51
+ }
52
+
53
+ private async Task Send ()
54
+ {
55
+ if (hubConnection is not null )
56
+ {// Invoke signalR hub method to configure plot sample rate
57
+ await hubConnection .SendAsync (" SetSamplesRate" , SamplePerTI , TimeIntervalInMS );
58
+ }
59
+ }
60
+
61
+ public bool IsConnected => hubConnection ? .State == HubConnectionState .Connected ;
62
+
63
+ public async Task ResetPlotStream ()
64
+ {
65
+ if (hubConnection is not null )
66
+ {// Invoke signalR hub method to reset plot configuration
67
+ await hubConnection .SendAsync (" ResetPlotDataSource" );
68
+ }
69
+ Samples = null ;
70
+ InvokeAsync (StateHasChanged );
71
+ }
72
+
73
+ public async void InitializeDataReception (PlotDataItem [] samples )
74
+ {
75
+ // Call back method that gets triggered on samples being received over signalR client
76
+ var samplesDiff = samples .Length - prevSampleCount ;
77
+ Debug .WriteLine ($" CLI LOG {DateTime .Now } :{samples .Length }, Diff : {samplesDiff }" );
78
+ if (samplesDiff != SamplePerTI )
79
+ {
80
+ Debug .WriteLine (" !!! Samples dropped !!!" );
81
+ // await ResetPlotStream();
82
+ }
83
+ prevSampleCount = 0 ;
84
+ SamplesList .AddRange (samples .ToList ());
85
+ if (SamplesList .Count > 0 )
86
+ {
87
+ Samples = SamplesList .ToArray ();
88
+ }
89
+ InvokeAsync (StateHasChanged );
90
+ }
91
+
92
+ public async ValueTask DisposeAsync ()
93
+ {
94
+ if (hubConnection is not null )
95
+ {
96
+ await hubConnection .DisposeAsync ();
97
+ }
98
+ }
99
+ }
0 commit comments