Invoke Multiple Groups (#1254)

This commit is contained in:
Mikael Mengistu 2018-01-05 13:57:19 -08:00 committed by GitHub
parent e1f9a65083
commit 8b34b7f2ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 145 additions and 0 deletions

View File

@ -162,6 +162,11 @@ namespace ChatSample
return _wrappedHubLifetimeManager.InvokeGroupAsync(groupName, methodName, args);
}
public override Task InvokeGroupsAsync(IReadOnlyList<string> groupNames, string methodName, object[] args)
{
return _wrappedHubLifetimeManager.InvokeGroupsAsync(groupNames, methodName, args);
}
public override Task InvokeUserAsync(string userId, string methodName, object[] args)
{
return _wrappedHubLifetimeManager.InvokeUserAsync(userId, methodName, args);

View File

@ -127,6 +127,29 @@ namespace Microsoft.AspNetCore.SignalR
return Task.CompletedTask;
}
public override Task InvokeGroupsAsync(IReadOnlyList<string> groupNames, string methodName, object[] args)
{
// Each task represents the list of tasks for each of the writes within a group
var tasks = new List<Task>();
var message = CreateInvocationMessage(methodName, args);
foreach (var groupName in groupNames)
{
if (string.IsNullOrEmpty(groupName))
{
throw new ArgumentException(nameof(groupName));
}
var group = _groups[groupName];
if (group != null)
{
tasks.Add(Task.WhenAll(group.Values.Select(c => c.WriteAsync(message))));
}
}
return Task.WhenAll(tasks);
}
public override Task InvokeGroupExceptAsync(string groupName, string methodName, object[] args, IReadOnlyList<string> excludedIds)
{
if (groupName == null)

View File

@ -20,6 +20,7 @@ namespace Microsoft.AspNetCore.SignalR
public dynamic Client(string connectionId) => new DynamicClientProxy(_clients.Client(connectionId));
public dynamic Clients(IReadOnlyList<string> connectionIds) => new DynamicClientProxy(_clients.Clients(connectionIds));
public dynamic Group(string groupName) => new DynamicClientProxy(_clients.Group(groupName));
public dynamic Groups(IReadOnlyList<string> groupNames) => new DynamicClientProxy(_clients.Groups(groupNames));
public dynamic GroupExcept(string groupName, IReadOnlyList<string> excludedIds) => new DynamicClientProxy(_clients.GroupExcept(groupName, excludedIds));
public dynamic OthersInGroup(string groupName) => new DynamicClientProxy(_clients.OthersInGroup(groupName));
public dynamic Others => new DynamicClientProxy(_clients.Others);

View File

@ -39,6 +39,11 @@ namespace Microsoft.AspNetCore.SignalR
return _hubClients.Group(groupName);
}
public IClientProxy Groups(IReadOnlyList<string> groupNames)
{
return _hubClients.Groups(groupNames);
}
public IClientProxy OthersInGroup(string groupName)
{
return _hubClients.GroupExcept(groupName, _currentConnectionId);

View File

@ -42,6 +42,11 @@ namespace Microsoft.AspNetCore.SignalR
return new MultipleClientProxy<THub>(_lifetimeManager, connectionIds);
}
public IClientProxy Groups(IReadOnlyList<string> groupNames)
{
return new MultipleGroupProxy<THub>(_lifetimeManager, groupNames);
}
public IClientProxy User(string userId)
{
return new UserProxy<THub>(_lifetimeManager, userId);

View File

@ -44,6 +44,11 @@ namespace Microsoft.AspNetCore.SignalR
return TypedClientBuilder<T>.Build(new GroupExceptProxy<THub>(_lifetimeManager, groupName, excludeIds));
}
public T Groups(IReadOnlyList<string> groupNames)
{
return TypedClientBuilder<T>.Build(new MultipleGroupProxy<THub>(_lifetimeManager, groupNames));
}
public virtual T User(string userId)
{
return TypedClientBuilder<T>.Build(new UserProxy<THub>(_lifetimeManager, userId));

View File

@ -22,6 +22,8 @@ namespace Microsoft.AspNetCore.SignalR
public abstract Task InvokeGroupAsync(string groupName, string methodName, object[] args);
public abstract Task InvokeGroupsAsync(IReadOnlyList<string> groupNames, string methodName, object[] args);
public abstract Task InvokeGroupExceptAsync(string groupName, string methodName, object[] args, IReadOnlyList<string> excludedIds);
public abstract Task InvokeUserAsync(string userId, string methodName, object[] args);

View File

@ -17,6 +17,8 @@ namespace Microsoft.AspNetCore.SignalR
T Group(string groupName);
T Groups(IReadOnlyList<string> groupNames);
T GroupExcept(string groupName, IReadOnlyList<string> excludeIds);
T User(string userId);

View File

@ -40,6 +40,23 @@ namespace Microsoft.AspNetCore.SignalR
}
}
public class MultipleGroupProxy<THub> : IClientProxy
{
private readonly HubLifetimeManager<THub> _lifetimeManager;
private IReadOnlyList<string> _groupNames;
public MultipleGroupProxy(HubLifetimeManager<THub> lifetimeManager, IReadOnlyList<string> groupNames)
{
_lifetimeManager = lifetimeManager;
_groupNames = groupNames;
}
public Task InvokeAsync(string method, params object[] args)
{
return _lifetimeManager.InvokeGroupsAsync(_groupNames, method, args);
}
}
public class GroupExceptProxy<THub> : IClientProxy
{
private readonly string _groupName;

View File

@ -42,6 +42,11 @@ namespace Microsoft.AspNetCore.SignalR
return TypedClientBuilder<T>.Build(_hubClients.Clients(connectionIds));
}
public T Groups(IReadOnlyList<string> groupNames)
{
return TypedClientBuilder<T>.Build(_hubClients.Groups(groupNames));
}
public T OthersInGroup(string groupName)
{
return TypedClientBuilder<T>.Build(_hubClients.OthersInGroup(groupName));

View File

@ -601,6 +601,26 @@ namespace Microsoft.AspNetCore.SignalR.Redis
return Task.WhenAll(publishTasks);
}
public override Task InvokeGroupsAsync(IReadOnlyList<string> groupNames, string methodName, object[] args)
{
if (groupNames == null)
{
throw new ArgumentNullException(nameof(groupNames));
}
var publishTasks = new List<Task>(groupNames.Count);
var message = new RedisInvocationMessage(target: methodName, arguments: args);
foreach (var groupName in groupNames)
{
if (!string.IsNullOrEmpty(groupName))
{
publishTasks.Add(PublishAsync(_channelNamePrefix + "." + groupName, message));
}
}
return Task.WhenAll(publishTasks);
}
private class LoggerTextWriter : TextWriter
{
private readonly ILogger _logger;

View File

@ -47,6 +47,11 @@ namespace Microsoft.AspNetCore.SignalR.Tests.HubEndpointTestUtils
return Clients.GroupExcept(groupName, excludedIds).InvokeAsync("Send", message);
}
public Task SendToMultipleGroups(string message, IReadOnlyList<string> groupNames)
{
return Clients.Groups(groupNames).InvokeAsync("Send", message);
}
public Task SendToOthersInGroup(string groupName, string message)
{
return Clients.OthersInGroup(groupName).InvokeAsync("Send", message);
@ -206,6 +211,11 @@ namespace Microsoft.AspNetCore.SignalR.Tests.HubEndpointTestUtils
return Clients.OthersInGroup(groupName).Send(message);
}
public Task SendToMultipleGroups(string message, IReadOnlyList<string> groupNames)
{
return Clients.Groups(groupNames).Send(message);
}
public Task BroadcastMethod(string message)
{
return Clients.All.Broadcast(message);
@ -277,6 +287,11 @@ namespace Microsoft.AspNetCore.SignalR.Tests.HubEndpointTestUtils
return Clients.GroupExcept(groupName, excludedIds).Send(message);
}
public Task SendToMultipleGroups(string message, IReadOnlyList<string> groupNames)
{
return Clients.Groups(groupNames).Send(message);
}
public Task SendToOthersInGroup(string groupName, string message)
{
return Clients.OthersInGroup(groupName).Send(message);

View File

@ -1123,6 +1123,46 @@ namespace Microsoft.AspNetCore.SignalR.Tests
}
}
[Theory]
[MemberData(nameof(HubTypes))]
public async Task InvokeMultipleGroups(Type hubType)
{
dynamic endPoint = HubEndPointTestUtils.GetHubEndpoint(hubType);
using (var firstClient = new TestClient())
using (var secondClient = new TestClient())
{
Task firstEndPointTask = endPoint.OnConnectedAsync(firstClient.Connection);
Task secondEndPointTask = endPoint.OnConnectedAsync(secondClient.Connection);
await Task.WhenAll(firstClient.Connected, secondClient.Connected).OrTimeout();
await secondClient.InvokeAsync(nameof(MethodHub.GroupAddMethod), "GroupA").OrTimeout();
await firstClient.InvokeAsync(nameof(MethodHub.GroupAddMethod), "GroupB").OrTimeout(); ;
var groupNames = new List<string> { "GroupA", "GroupB" };
await firstClient.SendInvocationAsync(nameof(MethodHub.SendToMultipleGroups), "test", groupNames).OrTimeout();
var hubMessage = await secondClient.ReadAsync().OrTimeout();
var invocation = Assert.IsType<InvocationMessage>(hubMessage);
Assert.Equal("Send", invocation.Target);
Assert.Single(invocation.Arguments);
Assert.Equal("test", invocation.Arguments[0]);
hubMessage = await firstClient.ReadAsync().OrTimeout();
invocation = Assert.IsType<InvocationMessage>(hubMessage);
Assert.Equal("Send", invocation.Target);
Assert.Single(invocation.Arguments);
Assert.Equal("test", invocation.Arguments[0]);
// kill the connections
firstClient.Dispose();
secondClient.Dispose();
await Task.WhenAll(firstEndPointTask, secondEndPointTask).OrTimeout();
}
}
[Fact]
public async Task RemoveFromGroupWhenNotInGroupDoesNotFail()
{