Skip to content

Commit 642a087

Browse files
committed
Add more hints about close connection reason.
1 parent cc6b7e2 commit 642a087

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

samples/Management/NegotiationServer/Controllers/NegotiateController.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ private async Task<ActionResult> NegotiateBase(string user, ServiceHubContext se
4040
return BadRequest("User ID is null or empty.");
4141
}
4242

43-
var negotiateResponse = await serviceHubContext.NegotiateAsync(new() { UserId = user });
43+
var negotiateResponse = await serviceHubContext.NegotiateAsync(new()
44+
{
45+
UserId = user/*,
46+
EnableDetailedErrors = true*/
47+
});
4448

4549
return new JsonResult(new Dictionary<string, string>()
4650
{

samples/Management/NegotiationServer/README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@ namespace NegotiationServer.Controllers
3535

3636
### Create instance of `ServiceHubContext`
3737

38-
`ServiceHubContext` provides methods to generate client endpoints and access tokens for SignalR clients to connect to Azure SignalR Service. Wrap `ServiceHubContext` into a [`IHostedService`](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-5.0) called `SignalRService` so that `ServiceHubContext` can be started and disposed when the web host started and stopped.
38+
`ServiceHubContext` provides methods to generate client endpoints and access tokens for SignalR clients to connect to Azure SignalR Service. Wrap `ServiceHubContext` into a [`IHostedService`](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-5.0) called `SignalRService` so that `ServiceHubContext` can be started and disposed when the web host starts and stops.
3939

40-
In `SignalRService` class, create the `ServiceHubContext`.
40+
In `SignalRService` class, create the `ServiceHubContext`. In the sample we have two hub, message hub and chat hub to demostrate how to set up multiple hubs. The chat hub is actually not used.
4141

4242
```C#
4343
public async Task StartAsync(CancellationToken cancellationToken)
4444
{
4545
using var serviceManager = new ServiceManagerBuilder()
4646
.WithConfiguration(_configuration)
47-
//or .WithOptions(o=>o.ConnectionString = _configuration["Azure:SignalR:ConnectionString"]
4847
.WithLoggerFactory(_loggerFactory)
4948
.BuildServiceManager();
50-
HubContext = await serviceManager.CreateHubContextAsync(Hub, cancellationToken);
49+
MessageHubContext = await serviceManager.CreateHubContextAsync(MessageHub, cancellationToken);
50+
ChatHubContext = await serviceManager.CreateHubContextAsync(ChatHub, cancellationToken);
5151
}
5252
```
5353

@@ -63,7 +63,6 @@ public Task StopAsync(CancellationToken cancellationToken) => HubContext?.Dispos
6363
In the `NegotiateController` class, provide the negotiation endpoint `/negotiate?user=<User ID>`.
6464

6565
We use the `_hubContext` to generate a client endpoint and an access token and return to SignalR client following [Negotiation Protocol](https://github.com/aspnet/SignalR/blob/master/specs/TransportProtocols.md#post-endpoint-basenegotiate-request), which will redirect the SignalR client to the service.
66-
6766
```C#
6867
[HttpPost("negotiate")]
6968
public async Task<ActionResult> Index(string user)
@@ -83,6 +82,16 @@ public async Task<ActionResult> Index(string user)
8382
}
8483
```
8584

85+
The sample above uses the default negotiation options. If you want to return detailed error messages to clients, you can set `EnableDetailedErrors` as follows:
86+
87+
```C#
88+
var negotiateResponse = await serviceHubContext.NegotiateAsync(new()
89+
{
90+
UserId = user,
91+
EnableDetailedErrors = true
92+
});
93+
```
94+
`EnableDetailedErrors` defaults to false because these exception messages can contain sensitive information.
8695
## Full Sample
8796

8897
The full negotiation server sample can be found [here](.). The usage of this sample can be found [here](<https://github.com/aspnet/AzureSignalR-samples/tree/master/samples/Management#start-the-negotiation-server>).

samples/Management/README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,17 @@ dotnet run
4444
Once the message publisher get started, use the command to send message
4545

4646
```
47-
send user <User ID List (Separated by ',')> <Message>
48-
send users <User List> <Message>
47+
send user <User Id> <Message>
48+
send users <User Id List (Seperated by ',')> <Message>
4949
send group <Group Name> <Message>
50-
send groups <Group List (Separated by ',')> <Message>
51-
usergroup add <User ID> <Group Name>
52-
usergroup remove <User ID> <Group Name>
50+
send groups <Group List (Seperated by ',')> <Message>
51+
usergroup add <User Id> <Group Name>
52+
usergroup remove <User Id> <Group Name>
5353
broadcast <Message>
54+
close <Connection ID> <Reason>
55+
checkexist connection <Connection ID>
56+
checkexist user <User ID>
57+
checkexist group <Group Name>
5458
```
5559
For example, type `broadcast hello`, and press keyboard `enter` to publish messages.
5660

samples/Management/SignalRClient/Program.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7-
using System.Runtime.CompilerServices;
87
using System.Threading.Tasks;
98
using Microsoft.AspNetCore.SignalR.Client;
109
using Microsoft.Extensions.CommandLineUtils;
@@ -62,8 +61,13 @@ static HubConnection CreateHubConnection(string hubEndpoint, string userId)
6261

6362
connection.Closed += ex =>
6463
{
65-
Console.WriteLine(ex);
66-
return Task.FromResult(0);
64+
Console.WriteLine($"The connection of '{userId}' is closed. Exception: {ex}");
65+
if (ex == null)
66+
{
67+
Console.WriteLine("If you expect non-null exception, you need to turn on 'EnableDetailedErrors' option during client negotiation.");
68+
}
69+
70+
return Task.CompletedTask;
6771
};
6872

6973
return connection;

0 commit comments

Comments
 (0)