From a7fb24350192f76e3539e60231d89a90eef7c13e Mon Sep 17 00:00:00 2001 From: FTWinston Date: Tue, 29 Aug 2017 00:31:12 +0100 Subject: [PATCH] Added typed HubContext --- .../HubContext`T.cs | 47 +++++++++++++++++++ .../IHubContext`T.cs | 14 ++++++ .../SignalRDependencyInjectionExtensions.cs | 1 + .../HubEndpointTests.cs | 31 ++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 src/Microsoft.AspNetCore.SignalR.Core/HubContext`T.cs create mode 100644 src/Microsoft.AspNetCore.SignalR.Core/IHubContext`T.cs diff --git a/src/Microsoft.AspNetCore.SignalR.Core/HubContext`T.cs b/src/Microsoft.AspNetCore.SignalR.Core/HubContext`T.cs new file mode 100644 index 0000000000..99b1aea9a4 --- /dev/null +++ b/src/Microsoft.AspNetCore.SignalR.Core/HubContext`T.cs @@ -0,0 +1,47 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Collections.Generic; + +namespace Microsoft.AspNetCore.SignalR +{ + public class HubContext : IHubContext, IHubClients + where THub : Hub + where T : class + { + private readonly HubLifetimeManager _lifetimeManager; + + public HubContext(HubLifetimeManager lifetimeManager) + { + _lifetimeManager = lifetimeManager; + All = TypedClientBuilder.Build(new AllClientProxy(_lifetimeManager)); + Groups = new GroupManager(lifetimeManager); + } + + public IHubClients Clients => this; + + public virtual T All { get; } + + public virtual IGroupManager Groups { get; } + + public T AllExcept(IReadOnlyList excludedIds) + { + return TypedClientBuilder.Build(new AllClientsExceptProxy(_lifetimeManager, excludedIds)); + } + + public virtual T Client(string connectionId) + { + return TypedClientBuilder.Build(new SingleClientProxy(_lifetimeManager, connectionId)); + } + + public virtual T Group(string groupName) + { + return TypedClientBuilder.Build(new GroupProxy(_lifetimeManager, groupName)); + } + + public virtual T User(string userId) + { + return TypedClientBuilder.Build(new UserProxy(_lifetimeManager, userId)); + } + } +} diff --git a/src/Microsoft.AspNetCore.SignalR.Core/IHubContext`T.cs b/src/Microsoft.AspNetCore.SignalR.Core/IHubContext`T.cs new file mode 100644 index 0000000000..305e8bed3d --- /dev/null +++ b/src/Microsoft.AspNetCore.SignalR.Core/IHubContext`T.cs @@ -0,0 +1,14 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace Microsoft.AspNetCore.SignalR +{ + public interface IHubContext + where THub : Hub + where T : class + { + IHubClients Clients { get; } + + IGroupManager Groups { get; } + } +} diff --git a/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs index f9d4038789..ba48638ad6 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs @@ -13,6 +13,7 @@ namespace Microsoft.Extensions.DependencyInjection services.AddSingleton(typeof(HubLifetimeManager<>), typeof(DefaultHubLifetimeManager<>)); services.AddSingleton(typeof(IHubProtocolResolver), typeof(DefaultHubProtocolResolver)); services.AddSingleton(typeof(IHubContext<>), typeof(HubContext<>)); + services.AddSingleton(typeof(IHubContext<,>), typeof(HubContext<,>)); services.AddSingleton(typeof(HubEndPoint<>), typeof(HubEndPoint<>)); services.AddScoped(typeof(IHubActivator<>), typeof(DefaultHubActivator<>)); diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs index 012d49b7e1..9b44396d1b 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs @@ -18,6 +18,7 @@ using Moq; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using Xunit; +using Microsoft.AspNetCore.Hosting; namespace Microsoft.AspNetCore.SignalR.Tests { @@ -239,6 +240,22 @@ namespace Microsoft.AspNetCore.SignalR.Tests } } + [Fact] + public async Task CanLoadHubContext() + { + var serviceProvider = CreateServiceProvider(); + var context = serviceProvider.GetRequiredService>(); + await context.Clients.All.InvokeAsync("Send", "test"); + } + + [Fact] + public async Task CanLoadTypedHubContext() + { + var serviceProvider = CreateServiceProvider(); + var context = serviceProvider.GetRequiredService>(); + await context.Clients.All.Send("test"); + } + [Fact] public async Task LifetimeManagerOnDisconnectedAsyncCalledIfLifetimeManagerOnConnectedAsyncThrows() { @@ -1579,5 +1596,19 @@ namespace Microsoft.AspNetCore.SignalR.Tests await base.OnConnectedAsync(); } } + + public interface ITypedHubClient + { + Task Send(string message); + } + + public class SimpleTypedHub : Hub + { + public override async Task OnConnectedAsync() + { + await Clients.All.Send($"{Context.ConnectionId} joined"); + await base.OnConnectedAsync(); + } + } } }