-
Notifications
You must be signed in to change notification settings - Fork 389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add strongly typed hub sample (Don't merge before feature is released) #168
Open
Y-Sindo
wants to merge
1
commit into
aspnet:main
Choose a base branch
from
Y-Sindo:zityang/management
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+203
−41
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add another program for strongly typed hub instead? frankly speaking, I don't see how appealing the strongly typed one is from the sample