Skip to content

Commit cc6b7e2

Browse files
committed
Add ClientManager usage to management sample
SignalR client output sample: ``` User 'User' with connection id 'CKZhiNk__nyv3Vrz8SnK_Q1448c44c1' connected. User: gets message from service: 'aaa' ``` MessagePublisher output sample: ``` *********Usage********* send user <User Id> <Message> send users <User Id List (Seperated by ',')> <Message> send group <Group Name> <Message> send groups <Group List (Seperated by ',')> <Message> usergroup add <User Id> <Group Name> usergroup remove <User Id> <Group Name> broadcast <Message> close <Connection ID> <Reason> checkexist connection <Connection ID> checkexist user <User ID> checkexist group <Group Name> *********************** > checkexist connection CKZhiNk__nyv3Vrz8SnK_Q1448c44c1 connection 'CKZhiNk__nyv3Vrz8SnK_Q1448c44c1' exists. > checkexist connection CKZhiNk__nyv3Vrz8SnK_Q1448c44c2 connection 'CKZhiNk__nyv3Vrz8SnK_Q1448c44c2' does not exist. > checkexist user User user 'User' exists. > send user User abc send message 'abc' to 'User' ```
1 parent 733b429 commit cc6b7e2

File tree

3 files changed

+55
-18
lines changed

3 files changed

+55
-18
lines changed

samples/Management/MessagePublisher/MessagePublisher.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ public Task SendMessages(string command, string receiver, string message)
7575
}
7676
}
7777

78+
public Task CloseConnection(string connectionId, string reason)
79+
{
80+
return _hubContext.ClientManager.CloseConnectionAsync(connectionId, reason);
81+
}
82+
83+
public Task<bool> CheckExist(string type, string id)
84+
{
85+
return type switch
86+
{
87+
"connection" => _hubContext.ClientManager.ConnectionExistsAsync(id),
88+
"user" => _hubContext.ClientManager.UserExistsAsync(id),
89+
"group" => _hubContext.ClientManager.UserExistsAsync(id),
90+
_ => throw new NotSupportedException(),
91+
};
92+
}
93+
7894
public Task DisposeAsync() => _hubContext?.DisposeAsync();
7995
}
8096
}

samples/Management/MessagePublisher/Program.cs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010

1111
namespace Microsoft.Azure.SignalR.Samples.Management
1212
{
13-
class Program
13+
public class Program
1414
{
15-
static void Main(string[] args)
15+
public static void Main(string[] args)
1616
{
17-
var app = new CommandLineApplication();
18-
app.FullName = "Azure SignalR Management Sample: Message Publisher";
17+
var app = new CommandLineApplication
18+
{
19+
FullName = "Azure SignalR Management Sample: Message Publisher"
20+
};
1921
app.HelpOption("--help");
2022
app.Description = "Message publisher using Azure SignalR Service Management SDK.";
2123

@@ -28,7 +30,7 @@ static void Main(string[] args)
2830
.Build();
2931

3032

31-
app.OnExecute(async() =>
33+
app.OnExecute(async () =>
3234
{
3335
var connectionString = connectionStringOption.Value() ?? configuration["Azure:SignalR:ConnectionString"];
3436

@@ -82,8 +84,8 @@ private static async Task StartAsync(MessagePublisher publisher)
8284

8385
if (args.Length == 2 && args[0].Equals("broadcast"))
8486
{
85-
Console.WriteLine($"broadcast message '{args[1]}'");
8687
await publisher.SendMessages(args[0], null, args[1]);
88+
Console.WriteLine($"broadcast message '{args[1]}'");
8789
}
8890
else if (args.Length == 4 && args[0].Equals("send"))
8991
{
@@ -96,10 +98,21 @@ private static async Task StartAsync(MessagePublisher publisher)
9698
var preposition = args[1] == "add" ? "to" : "from";
9799
Console.WriteLine($"{args[1]} user '{args[2]}' {preposition} group '{args[3]}'");
98100
}
101+
else if (args.Length == 3 && args[0] == "close")
102+
{
103+
await publisher.CloseConnection(args[1], args[2]);
104+
Console.WriteLine($"Close connection '{args[1]}' because '{args[2]}'");
105+
}
106+
else if (args.Length == 3 && args[0] == "checkexist")
107+
{
108+
var exist = await publisher.CheckExist(args[1].ToLowerInvariant(), args[2]);
109+
Console.WriteLine(exist ? $"{args[1]} '{args[2]}' exists." : $"{args[1]} '{args[2]}' does not exist.");
110+
}
99111
else
100112
{
101113
Console.WriteLine($"Can't recognize command {argLine}");
102114
}
115+
Console.Write("> ");
103116
}
104117
}
105118
finally
@@ -110,16 +123,21 @@ private static async Task StartAsync(MessagePublisher publisher)
110123

111124
private static void ShowHelp()
112125
{
113-
Console.WriteLine(
114-
"*********Usage*********\n" +
115-
"send user <User Id> <Message>\n" +
116-
"send users <User Id List (Seperated by ',')> <Message>\n" +
117-
"send group <Group Name> <Message>\n" +
118-
"send groups <Group List (Seperated by ',')> <Message>\n" +
119-
"usergroup add <User Id> <Group Name>\n" +
120-
"usergroup remove <User Id> <Group Name>\n" +
121-
"broadcast <Message>\n" +
122-
"***********************");
126+
Console.Write(
127+
@"*********Usage*********
128+
send user <User Id> <Message>
129+
send users <User Id List (Seperated by ',')> <Message>
130+
send group <Group Name> <Message>
131+
send groups <Group List (Seperated by ',')> <Message>
132+
usergroup add <User Id> <Group Name>
133+
usergroup remove <User Id> <Group Name>
134+
broadcast <Message>
135+
close <Connection ID> <Reason>
136+
checkexist connection <Connection ID>
137+
checkexist user <User ID>
138+
checkexist group <Group Name>
139+
***********************
140+
> ");
123141
}
124142

125143
private static void MissOptions()

samples/Management/SignalRClient/Program.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Program
1515
{
1616
private const string MessageHubEndpoint = "http://localhost:5000/Message";
1717
private const string Target = "Target";
18-
private const string DefaultUser = "User";
18+
private const string DefaultUser = "TestUser";
1919

2020
static void Main(string[] args)
2121
{
@@ -37,7 +37,10 @@ static void Main(string[] args)
3737
await Task.WhenAll(from conn in connections
3838
select conn.StartAsync());
3939

40-
Console.WriteLine($"{connections.Count} Client(s) started...");
40+
foreach (var (connection, userId) in connections.Zip(userIds))
41+
{
42+
Console.WriteLine($"User '{userId}' with connection id '{connection.ConnectionId}' connected.");
43+
}
4144
Console.ReadLine();
4245

4346
await Task.WhenAll(from conn in connections

0 commit comments

Comments
 (0)