-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathProgram.cs
88 lines (75 loc) · 3.3 KB
/
Program.cs
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.CommandLineUtils;
namespace SignalRClient
{
class Program
{
private const string HubEndpoint = "http://localhost:5000/Hub";
private const string StronglyTypedHubEndpoint = "http://localhost:5000/StronglyTypedHub";
private const string Target = "Target";
private const string DefaultUser = "TestUser";
static void Main(string[] args)
{
var app = new CommandLineApplication
{
FullName = "Azure SignalR Management Sample: SignalR Client Tool"
};
app.HelpOption("--help");
var userIdOption = app.Option("-u|--userIdList", "Set user ID list", CommandOptionType.MultipleValue, true);
var stronglyTypedOption = app.Option("-s|--strongly-typed", "Use strongly typed hub.", CommandOptionType.NoValue);
app.OnExecute(async () =>
{
var userIds = userIdOption.Values != null && userIdOption.Values.Count > 0 ? userIdOption.Values : new List<string>() { DefaultUser };
string hubEndpointToConnect;
if (stronglyTypedOption.HasValue())
{
hubEndpointToConnect = StronglyTypedHubEndpoint;
}
else
{
hubEndpointToConnect = HubEndpoint;
}
var connections = (from userId in userIds
select CreateHubConnection(hubEndpointToConnect, userId)).ToList();
await Task.WhenAll(from conn in connections
select conn.StartAsync());
foreach (var (connection, userId) in connections.Zip(userIds))
{
Console.WriteLine($"User '{userId}' with connection id '{connection.ConnectionId}' connected.");
}
Console.ReadLine();
await Task.WhenAll(from conn in connections
select conn.StopAsync());
return 0;
});
app.Execute(args);
}
static HubConnection CreateHubConnection(string hubEndpoint, string userId)
{
var url = hubEndpoint.TrimEnd('/') + $"?user={userId}";
var connection = new HubConnectionBuilder().WithUrl(url).Build();
connection.On(Target, (string message) =>
{
Console.WriteLine($"{userId}: gets message from service: '{message}'");
});
connection.Closed += ex =>
{
Console.Write($"The connection of '{userId}' is closed.");
//If you expect non-null exception, you need to turn on 'EnableDetailedErrors' option during client negotiation.
if (ex != null)
{
Console.Write($" Exception: {ex}");
}
Console.WriteLine();
return Task.CompletedTask;
};
return connection;
}
}
}