Skip to content

Commit 733b429

Browse files
authored
Add new management sample (#163)
* Add and update Management sample for 1.10.0 * Update and add md files * add file header * fix indention * fix * Remove Legacy Sample * fix * add another hub * Fix * Fix readme.md * Add more comments * fix doc * Add gitignore
1 parent bb7652a commit 733b429

File tree

19 files changed

+252
-175
lines changed

19 files changed

+252
-175
lines changed

samples/Management/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.sln

samples/Management/LegacySample/NegotiationServer/Controllers/NegotiateController.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

samples/Management/LegacySample/NegotiationServer/README.md

Lines changed: 0 additions & 79 deletions
This file was deleted.

samples/Management/LegacySample/NegotiationServer/appsettings.Development.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

samples/Management/LegacySample/NegotiationServer/appsettings.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

samples/Management/LegacySample/MessagePublisher/MessagePublisher.cs renamed to samples/Management/MessagePublisher/MessagePublisher.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ namespace Microsoft.Azure.SignalR.Samples.Management
1212
public class MessagePublisher
1313
{
1414
private const string Target = "Target";
15-
private const string HubName = "ManagementSampleHub";
15+
private const string HubName = "Message";
1616
private readonly string _connectionString;
1717
private readonly ServiceTransportType _serviceTransportType;
18-
private IServiceHubContext _hubContext;
18+
private ServiceHubContext _hubContext;
1919

2020
public MessagePublisher(string connectionString, ServiceTransportType serviceTransportType)
2121
{
@@ -29,9 +29,12 @@ public async Task InitAsync()
2929
{
3030
option.ConnectionString = _connectionString;
3131
option.ServiceTransportType = _serviceTransportType;
32-
}).Build();
32+
})
33+
//Uncomment the following line to get more logs
34+
//.WithLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole()))
35+
.BuildServiceManager();
3336

34-
_hubContext = await serviceManager.CreateHubContextAsync(HubName, new LoggerFactory());
37+
_hubContext = await serviceManager.CreateHubContextAsync(HubName, default);
3538
}
3639

3740
public Task ManageUserGroup(string command, string userId, string groupName)

samples/Management/LegacySample/MessagePublisher/MessagePublisher.csproj renamed to samples/Management/MessagePublisher/MessagePublisher.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<TargetFramework>net5</TargetFramework>
66
<UserSecretsId>ManagementSample</UserSecretsId>
77
<RootNamespace>Microsoft.Azure.SignalR.Samples.Management</RootNamespace>
88
</PropertyGroup>
@@ -14,4 +14,4 @@
1414
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
1515
</ItemGroup>
1616

17-
</Project>
17+
</Project>

samples/Management/LegacySample/MessagePublisher/Program.cs renamed to samples/Management/MessagePublisher/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private static void ShowHelp()
124124

125125
private static void MissOptions()
126126
{
127-
Console.WriteLine("Miss required options: Connection string and Hub must be set");
127+
Console.WriteLine("Miss required options: Connection string must be set");
128128
}
129129
}
130130
}

samples/Management/LegacySample/MessagePublisher/README.md renamed to samples/Management/MessagePublisher/README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,27 @@ This sample shows how to use [Microsoft.Azure.SignalR.Management](https://www.nu
88
### Add Management SDK to your project
99

1010
```
11-
dotnet add package Microsoft.Azure.SignalR.Management -v 1.0.0-*
11+
dotnet add package Microsoft.Azure.SignalR.Management -v 1.*
1212
```
1313

14-
### Create instance of `IServiceManager`
14+
### Create instance of `ServiceManager`
1515

16-
The `IServiceManager` is able to manage your Azure SignalR Service from your connection string.
16+
The `ServiceManager` is able to manage your Azure SignalR Service.
1717

1818
```c#
19-
var serviceManager = new ServiceManagerBuilder()
20-
.WithOptions(option =>
21-
{
22-
option.ConnectionString = "<Your Connection String>";
23-
})
24-
.Build();
19+
var serviceManager = new ServiceManagerBuilder().WithOptions(option =>
20+
{
21+
option.ConnectionString = _connectionString;
22+
option.ServiceTransportType = _serviceTransportType;
23+
})
24+
//Uncomment the following line to get more logs
25+
//.WithLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole()))
26+
.BuildServiceManager();
2527
```
2628

27-
### Create instance of `IServiceHubContext`
29+
### Create instance of `ServiceHubContext`
2830

29-
The `IServiceHubContext` is used to publish messages to a specific hub.
31+
The `ServiceHubContext` is used to publish messages to a specific hub.
3032

3133
```C#
3234
var hubContext = await serviceManager.CreateHubContextAsync("<Your Hub Name>");
@@ -40,7 +42,7 @@ Once you create the `hubContext`, you can use it to publish messages to a given
4042
// broadcast
4143
hubContext.Clients.All.SendAsync("<Your SignalR Client Callback>", "<Arg1>", "<Arg2>", ...);
4244

43-
// send to a user
45+
// send to a user
4446
hubContext.Clients.User("<User ID>").SendAsync("<Your SignalR Client Callback>", "<Arg1>", "<Arg2>", ...);
4547

4648
// send to users
@@ -63,7 +65,7 @@ hubContext.UserGroups.RemoveFromGroupAsync("<User ID>", "<Group Name>");
6365

6466
All features can be found [here](<https://github.com/Azure/azure-signalr/blob/dev/docs/management-sdk-guide.md#features>).
6567

66-
### Dispose the instance of `IServiceHubContext`
68+
### Dispose the instance of `ServiceHubContext`
6769

6870
```c#
6971
await hubContext.DisposeAsync();
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Collections.Generic;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.Azure.SignalR.Management;
8+
9+
namespace NegotiationServer.Controllers
10+
{
11+
[ApiController]
12+
public class NegotiateController : ControllerBase
13+
{
14+
private readonly ServiceHubContext _messageHubContext;
15+
private readonly ServiceHubContext _chatHubContext;
16+
17+
public NegotiateController(IHubContextStore store)
18+
{
19+
_messageHubContext = store.MessageHubContext;
20+
_chatHubContext = store.ChatHubContext;
21+
}
22+
23+
[HttpPost("message/negotiate")]
24+
public Task<ActionResult> MessageHubNegotiate(string user)
25+
{
26+
return NegotiateBase(user, _messageHubContext);
27+
}
28+
29+
//This API is not used. Just demonstrate a way to have multiple hubs.
30+
[HttpPost("chat/negotiate")]
31+
public Task<ActionResult> ChatHubNegotiate(string user)
32+
{
33+
return NegotiateBase(user, _chatHubContext);
34+
}
35+
36+
private async Task<ActionResult> NegotiateBase(string user, ServiceHubContext serviceHubContext)
37+
{
38+
if (string.IsNullOrEmpty(user))
39+
{
40+
return BadRequest("User ID is null or empty.");
41+
}
42+
43+
var negotiateResponse = await serviceHubContext.NegotiateAsync(new() { UserId = user });
44+
45+
return new JsonResult(new Dictionary<string, string>()
46+
{
47+
{ "url", negotiateResponse.Url },
48+
{ "accessToken", negotiateResponse.AccessToken }
49+
});
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)