3
3
using Microsoft . AspNetCore . SignalR . Client ;
4
4
5
5
6
- List < HubConnection > _connections = new ( ) ;
6
+ List < HubConnection > _connections = [ ] ;
7
7
string message = $ "Hello World";
8
8
await InitializeAsync ( ) ;
9
9
Console . WriteLine ( "Sending some messages as a warm-up" ) ;
10
- await Parallel . ForAsync ( 0L , 100000 , new ParallelOptions { TaskScheduler = TaskScheduler . Default , MaxDegreeOfParallelism = 1024 } , async ( _ , _ ) =>
10
+ await Parallel . ForAsync ( 0L , 100000 , new ParallelOptions { TaskScheduler = TaskScheduler . Default , MaxDegreeOfParallelism = 1024 } , async ( _ , cancellationToken ) =>
11
11
{
12
12
HubConnection conn1 = _connections [ Random . Shared . Next ( 0 , _connections . Count ) ] ;
13
13
HubConnection conn2 = _connections [ Random . Shared . Next ( 0 , _connections . Count ) ] ;
14
- string response = await conn2 . InvokeAsync < string > ( "SendToClient" , message , conn1 . ConnectionId ) ;
14
+ string response = await conn2 . InvokeAsync < string > ( "SendToClient" , message , conn1 . ConnectionId , cancellationToken ) ;
15
15
} ) ;
16
16
17
17
var timer = Stopwatch . StartNew ( ) ;
18
- using CancellationTokenSource cts = new CancellationTokenSource ( TimeSpan . FromSeconds ( 10 ) ) ;
18
+ using var cts = new CancellationTokenSource ( TimeSpan . FromSeconds ( 10 ) ) ;
19
19
int responsesReceived = 0 ;
20
20
Console . WriteLine ( "Starting invoke test" ) ;
21
21
try
22
22
{
23
- await Parallel . ForAsync ( 0L , Int64 . MaxValue , new ParallelOptions { TaskScheduler = TaskScheduler . Default , MaxDegreeOfParallelism = 1024 , CancellationToken = cts . Token } , async ( _ , _ ) =>
23
+ await Parallel . ForAsync ( 0L , long . MaxValue , new ParallelOptions { TaskScheduler = TaskScheduler . Default , MaxDegreeOfParallelism = 1024 , CancellationToken = cts . Token } , async ( _ , cancellationToken ) =>
24
24
{
25
25
HubConnection conn1 = _connections [ Random . Shared . Next ( 0 , _connections . Count ) ] ;
26
26
HubConnection conn2 = _connections [ Random . Shared . Next ( 0 , _connections . Count ) ] ;
27
- string response = await conn2 . InvokeAsync < string > ( "SendToClient" , message , conn1 . ConnectionId ) ;
27
+ string response = await conn2 . InvokeAsync < string > ( "SendToClient" , message , conn1 . ConnectionId , cancellationToken ) ;
28
28
responsesReceived ++ ;
29
29
} ) ;
30
30
}
34
34
}
35
35
36
36
timer = Stopwatch . StartNew ( ) ;
37
- using CancellationTokenSource cts2 = new CancellationTokenSource ( TimeSpan . FromSeconds ( 10 ) ) ;
37
+ using var cts2 = new CancellationTokenSource ( TimeSpan . FromSeconds ( 10 ) ) ;
38
38
responsesReceived = 0 ;
39
39
Console . WriteLine ( "Starting send all test" ) ;
40
40
try
41
41
{
42
- await Parallel . ForAsync ( 0L , Int64 . MaxValue , new ParallelOptions { TaskScheduler = TaskScheduler . Default , MaxDegreeOfParallelism = 1024 , CancellationToken = cts2 . Token } , async ( _ , _ ) =>
42
+ await Parallel . ForAsync ( 0L , long . MaxValue , new ParallelOptions { TaskScheduler = TaskScheduler . Default , MaxDegreeOfParallelism = 1024 , CancellationToken = cts2 . Token } , async ( _ , cancellationToken ) =>
43
43
{
44
44
HubConnection conn1 = _connections [ Random . Shared . Next ( 0 , _connections . Count ) ] ;
45
- await conn1 . InvokeAsync ( "SendToAllClients" , message ) ;
45
+ await conn1 . InvokeAsync ( "SendToAllClients" , message , cancellationToken ) ;
46
46
responsesReceived ++ ;
47
47
} ) ;
48
48
}
52
52
}
53
53
54
54
timer = Stopwatch . StartNew ( ) ;
55
- using CancellationTokenSource cts3 = new CancellationTokenSource ( TimeSpan . FromSeconds ( 10 ) ) ;
55
+ using var cts3 = new CancellationTokenSource ( TimeSpan . FromSeconds ( 10 ) ) ;
56
56
responsesReceived = 0 ;
57
57
Console . WriteLine ( "Starting send others test" ) ;
58
58
try
59
59
{
60
- await Parallel . ForAsync ( 0L , Int64 . MaxValue , new ParallelOptions { TaskScheduler = TaskScheduler . Default , MaxDegreeOfParallelism = 1024 , CancellationToken = cts3 . Token } , async ( _ , _ ) =>
60
+ await Parallel . ForAsync ( 0L , long . MaxValue , new ParallelOptions { TaskScheduler = TaskScheduler . Default , MaxDegreeOfParallelism = 1024 , CancellationToken = cts3 . Token } , async ( _ , cancellationToken ) =>
61
61
{
62
62
HubConnection conn1 = _connections [ Random . Shared . Next ( 0 , _connections . Count ) ] ;
63
- await conn1 . InvokeAsync ( "SendToOthers" , message ) ;
63
+ await conn1 . InvokeAsync ( "SendToOthers" , message , cancellationToken ) ;
64
64
responsesReceived ++ ;
65
65
} ) ;
66
66
}
@@ -86,10 +86,7 @@ async Task InitializeAsync()
86
86
for ( int i = 0 ; i < 100 ; i ++ )
87
87
{
88
88
HubConnection conn = await GetConnection ( ) ;
89
- conn . On < string , string , string > ( "Hello" , ( message , connectionId ) =>
90
- {
91
- return $ "{ conn . ConnectionId } got { message } from { connectionId } ";
92
- } ) ;
89
+ conn . On < string , string , string > ( "Hello" , ( message , connectionId ) => $ "{ conn . ConnectionId } got { message } from { connectionId } ") ;
93
90
94
91
conn . On < string > ( "Send" , ( message ) =>
95
92
{
0 commit comments