-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* NegotiationServer * Change another hub into strongly typed hub * MessagePublisher * Extract an interface `IMessagePublisher` from the `MessagePublisher`, and implement a `StronglyTypedMessagePublisher`. * Add an option "-s|--strongly-typed-hub" to decide which `IMessagePublisher` to use. * SignalRClient * Add an option "-s|--strongly-typed-hub" to decide connect to which hub.
- Loading branch information
Showing
11 changed files
with
203 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.SignalR.Samples.Management | ||
{ | ||
public interface IMessageClient | ||
{ | ||
Task Target(string message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.SignalR.Samples.Management | ||
{ | ||
public interface IMessagePublisher | ||
{ | ||
Task<bool> CheckExist(string type, string id); | ||
Task CloseConnection(string connectionId, string reason); | ||
Task DisposeAsync(); | ||
Task InitAsync(); | ||
Task ManageUserGroup(string command, string userId, string groupName); | ||
Task SendMessages(string command, string receiver, string message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
samples/Management/MessagePublisher/StronglyTypedMessagePublisher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// 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.Threading.Tasks; | ||
using Microsoft.Azure.SignalR.Management; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.Azure.SignalR.Samples.Management | ||
{ | ||
public class StronglyTypedMessagePublisher : IMessagePublisher | ||
{ | ||
private const string HubName = "StronglyTypedHub"; | ||
private readonly string _connectionString; | ||
private readonly ServiceTransportType _serviceTransportType; | ||
private ServiceHubContext<IMessageClient> _hubContext; | ||
|
||
public StronglyTypedMessagePublisher(string connectionString, ServiceTransportType serviceTransportType) | ||
{ | ||
_connectionString = connectionString; | ||
_serviceTransportType = serviceTransportType; | ||
} | ||
|
||
public async Task InitAsync() | ||
{ | ||
var serviceManager = new ServiceManagerBuilder().WithOptions(option => | ||
{ | ||
option.ConnectionString = _connectionString; | ||
option.ServiceTransportType = _serviceTransportType; | ||
}) | ||
//Uncomment the following line to get more logs | ||
.WithLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole())) | ||
.BuildServiceManager(); | ||
|
||
_hubContext = await serviceManager.CreateHubContextAsync<IMessageClient>(HubName, default); | ||
} | ||
|
||
|
||
public Task ManageUserGroup(string command, string userId, string groupName) | ||
{ | ||
switch (command) | ||
{ | ||
case "add": | ||
return _hubContext.UserGroups.AddToGroupAsync(userId, groupName); | ||
case "remove": | ||
return _hubContext.UserGroups.RemoveFromGroupAsync(userId, groupName); | ||
default: | ||
Console.WriteLine($"Can't recognize command {command}"); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
public Task SendMessages(string command, string receiver, string message) | ||
{ | ||
switch (command) | ||
{ | ||
case "broadcast": | ||
return _hubContext.Clients.All.Target(message); | ||
case "user": | ||
var userId = receiver; | ||
return _hubContext.Clients.User(userId).Target(message); | ||
case "users": | ||
var userIds = receiver.Split(','); | ||
return _hubContext.Clients.Users(userIds).Target(message); | ||
case "group": | ||
var groupName = receiver; | ||
return _hubContext.Clients.Group(groupName).Target(message); | ||
case "groups": | ||
var groupNames = receiver.Split(','); | ||
return _hubContext.Clients.Groups(groupNames).Target(message); | ||
default: | ||
Console.WriteLine($"Can't recognize command {command}"); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
public Task CloseConnection(string connectionId, string reason) | ||
{ | ||
return _hubContext.ClientManager.CloseConnectionAsync(connectionId, reason); | ||
} | ||
|
||
public Task<bool> CheckExist(string type, string id) | ||
{ | ||
return type switch | ||
{ | ||
"connection" => _hubContext.ClientManager.ConnectionExistsAsync(id), | ||
"user" => _hubContext.ClientManager.UserExistsAsync(id), | ||
"group" => _hubContext.ClientManager.UserExistsAsync(id), | ||
_ => throw new NotSupportedException(), | ||
}; | ||
} | ||
|
||
public Task DisposeAsync() => _hubContext?.DisposeAsync().AsTask(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Threading.Tasks; | ||
|
||
namespace NegotiationServer | ||
{ | ||
// Copied from Message Publisher | ||
public interface IMessageClient | ||
{ | ||
Task Target(string message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters