From c8bd72be361b84ded1b7e6c60f39f725500b867e Mon Sep 17 00:00:00 2001 From: Mikael Mengistu Date: Mon, 18 Dec 2017 15:27:56 -0800 Subject: [PATCH] Others in Group (#1230) --- .../DynamicHubClients.cs | 1 + .../HubCallerClients.cs | 5 ++ .../IHubCallerClients`T.cs | 2 + .../TypedHubClients.cs | 5 ++ .../RedisHubLifetimeManagerTests.cs | 2 +- .../HubEndpointTests.cs | 65 +++++++++++++++++++ 6 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNetCore.SignalR.Core/DynamicHubClients.cs b/src/Microsoft.AspNetCore.SignalR.Core/DynamicHubClients.cs index 14c58be703..604a851f80 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/DynamicHubClients.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/DynamicHubClients.cs @@ -20,6 +20,7 @@ namespace Microsoft.AspNetCore.SignalR public dynamic Client(string connectionId) => new DynamicClientProxy(_clients.Client(connectionId)); public dynamic Group(string groupName) => new DynamicClientProxy(_clients.Group(groupName)); public dynamic GroupExcept(string groupName, IReadOnlyList excludedIds) => new DynamicClientProxy(_clients.GroupExcept(groupName, excludedIds)); + public dynamic OthersInGroup(string groupName) => new DynamicClientProxy(_clients.OthersInGroup(groupName)); public dynamic Others => new DynamicClientProxy(_clients.Others); public dynamic User(string userId) => new DynamicClientProxy(_clients.User(userId)); } diff --git a/src/Microsoft.AspNetCore.SignalR.Core/HubCallerClients.cs b/src/Microsoft.AspNetCore.SignalR.Core/HubCallerClients.cs index b90d7df5c8..936c2e4d66 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/HubCallerClients.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/HubCallerClients.cs @@ -39,6 +39,11 @@ namespace Microsoft.AspNetCore.SignalR return _hubClients.Group(groupName); } + public IClientProxy OthersInGroup(string groupName) + { + return _hubClients.GroupExcept(groupName, _currentConnectionId); + } + public IClientProxy GroupExcept(string groupName, IReadOnlyList excludeIds) { return _hubClients.GroupExcept(groupName, excludeIds); diff --git a/src/Microsoft.AspNetCore.SignalR.Core/IHubCallerClients`T.cs b/src/Microsoft.AspNetCore.SignalR.Core/IHubCallerClients`T.cs index 471a9e5584..edd3ab8c80 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/IHubCallerClients`T.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/IHubCallerClients`T.cs @@ -8,5 +8,7 @@ namespace Microsoft.AspNetCore.SignalR T Caller { get; } T Others { get; } + + T OthersInGroup(string groupName); } } diff --git a/src/Microsoft.AspNetCore.SignalR.Core/TypedHubClients.cs b/src/Microsoft.AspNetCore.SignalR.Core/TypedHubClients.cs index 68fb565344..ac68e41380 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/TypedHubClients.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/TypedHubClients.cs @@ -37,6 +37,11 @@ namespace Microsoft.AspNetCore.SignalR return TypedClientBuilder.Build(_hubClients.GroupExcept(groupName, excludeIds)); } + public T OthersInGroup(string groupName) + { + return TypedClientBuilder.Build(_hubClients.OthersInGroup(groupName)); + } + public T User(string userId) { return TypedClientBuilder.Build(_hubClients.User(userId)); diff --git a/test/Microsoft.AspNetCore.SignalR.Redis.Tests/RedisHubLifetimeManagerTests.cs b/test/Microsoft.AspNetCore.SignalR.Redis.Tests/RedisHubLifetimeManagerTests.cs index aba7461fdc..bd10c29db0 100644 --- a/test/Microsoft.AspNetCore.SignalR.Redis.Tests/RedisHubLifetimeManagerTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Redis.Tests/RedisHubLifetimeManagerTests.cs @@ -127,10 +127,10 @@ namespace Microsoft.AspNetCore.SignalR.Redis.Tests await manager.InvokeGroupExceptAsync("gunit", "Hello", new object[] { "World" }, excludedIds).OrTimeout(); await AssertMessageAsync(client1); + Assert.Null(client2.TryRead()); await connection1.DisposeAsync().OrTimeout(); await connection2.DisposeAsync().OrTimeout(); - Assert.Null(client2.TryRead()); } } diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs index a98926904a..0d25189d41 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs @@ -1044,6 +1044,56 @@ namespace Microsoft.AspNetCore.SignalR.Tests } } + [Theory] + [MemberData(nameof(HubTypes))] + public async Task SendToOthersInGroup(Type hubType) + { + var serviceProvider = CreateServiceProvider(); + + dynamic endPoint = serviceProvider.GetService(GetEndPointType(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(); + + var result = (await firstClient.InvokeAsync("GroupSendMethod", "testGroup", "test").OrTimeout()).Result; + + // check that 'firstConnection' hasn't received the group send + Assert.Null(firstClient.TryRead()); + + // check that 'secondConnection' hasn't received the group send + Assert.Null(secondClient.TryRead()); + + await firstClient.InvokeAsync(nameof(MethodHub.GroupAddMethod), "testGroup").OrTimeout(); + await secondClient.InvokeAsync(nameof(MethodHub.GroupAddMethod), "testGroup").OrTimeout(); + + await firstClient.SendInvocationAsync("SendToOthersInGroup", "testGroup", "test").OrTimeout(); + + // check that 'secondConnection' has received the group send + var hubMessage = await secondClient.ReadAsync().OrTimeout(); + var invocation = Assert.IsType(hubMessage); + Assert.Equal("Send", invocation.Target); + Assert.Single(invocation.Arguments); + Assert.Equal("test", invocation.Arguments[0]); + + // Check that first client only got the completion message + hubMessage = await firstClient.ReadAsync().OrTimeout(); + Assert.IsType(hubMessage); + + Assert.Null(firstClient.TryRead()); + + // kill the connections + firstClient.Dispose(); + secondClient.Dispose(); + + await Task.WhenAll(firstEndPointTask, secondEndPointTask).OrTimeout(); + } + } + [Fact] public async Task RemoveFromGroupWhenNotInGroupDoesNotFail() { @@ -1683,6 +1733,11 @@ namespace Microsoft.AspNetCore.SignalR.Tests return Clients.GroupExcept(groupName, excludedIds).Send(message); } + public Task SendToOthersInGroup(string groupName, string message) + { + return Clients.OthersInGroup(groupName).Send(message); + } + public Task BroadcastMethod(string message) { return Clients.All.Broadcast(message); @@ -1876,6 +1931,11 @@ namespace Microsoft.AspNetCore.SignalR.Tests return Clients.GroupExcept(groupName, excludedIds).Send(message); } + public Task SendToOthersInGroup(string groupName, string message) + { + return Clients.OthersInGroup(groupName).Send(message); + } + public Task BroadcastMethod(string message) { return Clients.All.Broadcast(message); @@ -2012,6 +2072,11 @@ namespace Microsoft.AspNetCore.SignalR.Tests return Clients.GroupExcept(groupName, excludedIds).InvokeAsync("Send", message); } + public Task SendToOthersInGroup(string groupName, string message) + { + return Clients.OthersInGroup(groupName).InvokeAsync("Send", message); + } + public Task BroadcastMethod(string message) { return Clients.All.InvokeAsync("Broadcast", message);