Skip to content

Commit 569741c

Browse files
[release/7.0] Fix sending to another client from OnConnectedAsync (#47204)
* Fix sending to another client from OnConnectedAsync * Update src/SignalR/server/SignalR/test/HubConnectionHandlerTests.cs --------- Co-authored-by: Brennan Conroy <[email protected]>
1 parent 4110466 commit 569741c

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/SignalR/server/Core/src/Internal/HubCallerClients.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public ISingleClientProxy Client(string connectionId)
5050
{
5151
if (!InvokeAllowed)
5252
{
53-
return new NoInvokeSingleClientProxy(_hubClients.Client(_connectionId));
53+
return new NoInvokeSingleClientProxy(_hubClients.Client(connectionId));
5454
}
5555
return new SingleClientProxy(_hubClients.Client(connectionId), this);
5656
}

src/SignalR/server/SignalR/test/HubConnectionHandlerTestUtils/Hubs.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,3 +1396,15 @@ public void ManyParams(int a1, string a2, bool a3, float a4, string a5, int a6,
13961396
int a64, [FromService] Service1 service)
13971397
{ }
13981398
}
1399+
1400+
public class OnConnectedSendToClientHub : Hub
1401+
{
1402+
public override async Task OnConnectedAsync()
1403+
{
1404+
string id = Context.GetHttpContext()?.Request.Query["client"] ?? string.Empty;
1405+
if (!string.IsNullOrEmpty(id))
1406+
{
1407+
await Clients.Client(id).SendAsync("Test", 1);
1408+
}
1409+
}
1410+
}

src/SignalR/server/SignalR/test/HubConnectionHandlerTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4891,6 +4891,39 @@ public void TooManyParametersWithServiceThrows()
48914891
() => serviceProvider.GetService<HubConnectionHandler<TooManyParamsHub>>());
48924892
}
48934893

4894+
[Fact]
4895+
public async Task SendToAnotherClientFromOnConnectedAsync()
4896+
{
4897+
var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(provider =>
4898+
{
4899+
provider.AddSignalR(options =>
4900+
{
4901+
options.EnableDetailedErrors = true;
4902+
});
4903+
});
4904+
var connectionHandler = serviceProvider.GetService<HubConnectionHandler<OnConnectedSendToClientHub>>();
4905+
4906+
using (var client1 = new TestClient())
4907+
using (var client2 = new TestClient())
4908+
{
4909+
var httpContext = new DefaultHttpContext();
4910+
httpContext.Request.QueryString = new QueryString($"?client={client1.Connection.ConnectionId}");
4911+
var feature = new TestHttpContextFeature
4912+
{
4913+
HttpContext = httpContext
4914+
};
4915+
client2.Connection.Features.Set<IHttpContextFeature>(feature);
4916+
4917+
var connectionHandlerTask = await client1.ConnectAsync(connectionHandler).DefaultTimeout();
4918+
_ = await client2.ConnectAsync(connectionHandler).DefaultTimeout();
4919+
4920+
var message = Assert.IsType<InvocationMessage>(await client1.ReadAsync().DefaultTimeout());
4921+
Assert.Single(message.Arguments);
4922+
Assert.Equal(1L, message.Arguments[0]);
4923+
Assert.Equal("Test", message.Target);
4924+
}
4925+
}
4926+
48944927
private class CustomHubActivator<THub> : IHubActivator<THub> where THub : Hub
48954928
{
48964929
public int ReleaseCount;

0 commit comments

Comments
 (0)