Generics overload

- Changed IHubConnectionContext and friends to be generic.
- Hub by default is Hub<IClientProxy>. We'll enable dynamic and arbitrary TClients in another commit.
This commit is contained in:
David Fowler 2016-11-05 10:53:54 -07:00
parent 9c10b89fa8
commit 217223e636
6 changed files with 40 additions and 21 deletions

View File

@ -3,14 +3,19 @@ using System.Threading.Tasks;
namespace Microsoft.AspNetCore.SignalR namespace Microsoft.AspNetCore.SignalR
{ {
public class Hub : IDisposable public class Hub : Hub<IClientProxy>
{
}
public class Hub<TClient> : IDisposable
{ {
private bool _disposed; private bool _disposed;
private IHubConnectionContext _clients; private IHubConnectionContext<TClient> _clients;
private HubCallerContext _context; private HubCallerContext _context;
private IGroupManager _groups; private IGroupManager _groups;
public IHubConnectionContext Clients public IHubConnectionContext<TClient> Clients
{ {
get get
{ {

View File

@ -7,15 +7,13 @@ namespace Microsoft.AspNetCore.SignalR
{ {
public HubCallerContext(Connection connection) public HubCallerContext(Connection connection)
{ {
ConnectionId = connection.ConnectionId;
User = connection.User;
Connection = connection; Connection = connection;
} }
public Connection Connection { get; } public Connection Connection { get; }
public ClaimsPrincipal User { get; } public ClaimsPrincipal User => Connection.User;
public string ConnectionId { get; } public string ConnectionId => Connection.ConnectionId;
} }
} }

View File

@ -5,7 +5,7 @@ using System.Threading.Tasks;
namespace Microsoft.AspNetCore.SignalR namespace Microsoft.AspNetCore.SignalR
{ {
public class HubContext<THub> : IHubContext<THub>, IHubConnectionContext public class HubContext<THub> : IHubContext<THub>, IHubConnectionContext<IClientProxy>
{ {
private readonly HubLifetimeManager<THub> _lifetimeManager; private readonly HubLifetimeManager<THub> _lifetimeManager;
private readonly AllClientProxy<THub> _all; private readonly AllClientProxy<THub> _all;
@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.SignalR
_all = new AllClientProxy<THub>(_lifetimeManager); _all = new AllClientProxy<THub>(_lifetimeManager);
} }
public IHubConnectionContext Clients => this; public IHubConnectionContext<IClientProxy> Clients => this;
public virtual IClientProxy All => _all; public virtual IClientProxy All => _all;

View File

@ -10,22 +10,34 @@ using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.SignalR namespace Microsoft.AspNetCore.SignalR
{ {
public class HubEndPoint<THub> : EndPoint where THub : Hub public class HubEndPoint<THub> : HubEndPoint<THub, IClientProxy> where THub : Hub<IClientProxy>
{
public HubEndPoint(HubLifetimeManager<THub> lifetimeManager,
IHubContext<THub> hubContext,
InvocationAdapterRegistry registry,
ILogger<HubEndPoint<THub>> logger,
IServiceScopeFactory serviceScopeFactory) : base(lifetimeManager, hubContext, registry, logger, serviceScopeFactory)
{
}
}
public class HubEndPoint<THub, TClient> : EndPoint where THub : Hub<TClient>
{ {
private readonly Dictionary<string, Func<Connection, InvocationDescriptor, Task<InvocationResultDescriptor>>> _callbacks private readonly Dictionary<string, Func<Connection, InvocationDescriptor, Task<InvocationResultDescriptor>>> _callbacks
= new Dictionary<string, Func<Connection, InvocationDescriptor, Task<InvocationResultDescriptor>>>(StringComparer.OrdinalIgnoreCase); = new Dictionary<string, Func<Connection, InvocationDescriptor, Task<InvocationResultDescriptor>>>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, Type[]> _paramTypes = new Dictionary<string, Type[]>(); private readonly Dictionary<string, Type[]> _paramTypes = new Dictionary<string, Type[]>();
private readonly HubLifetimeManager<THub> _lifetimeManager; private readonly HubLifetimeManager<THub> _lifetimeManager;
private readonly IHubContext<THub> _hubContext; private readonly IHubContext<THub, TClient> _hubContext;
private readonly ILogger<HubEndPoint<THub>> _logger; private readonly ILogger<HubEndPoint<THub, TClient>> _logger;
private readonly InvocationAdapterRegistry _registry; private readonly InvocationAdapterRegistry _registry;
private readonly IServiceScopeFactory _serviceScopeFactory; private readonly IServiceScopeFactory _serviceScopeFactory;
public HubEndPoint(HubLifetimeManager<THub> lifetimeManager, public HubEndPoint(HubLifetimeManager<THub> lifetimeManager,
IHubContext<THub> hubContext, IHubContext<THub, TClient> hubContext,
InvocationAdapterRegistry registry, InvocationAdapterRegistry registry,
ILogger<HubEndPoint<THub>> logger, ILogger<HubEndPoint<THub, TClient>> logger,
IServiceScopeFactory serviceScopeFactory) IServiceScopeFactory serviceScopeFactory)
{ {
_lifetimeManager = lifetimeManager; _lifetimeManager = lifetimeManager;

View File

@ -1,13 +1,13 @@
namespace Microsoft.AspNetCore.SignalR namespace Microsoft.AspNetCore.SignalR
{ {
public interface IHubConnectionContext public interface IHubConnectionContext<TClient>
{ {
IClientProxy All { get; } TClient All { get; }
IClientProxy Client(string connectionId); TClient Client(string connectionId);
IClientProxy Group(string groupName); TClient Group(string groupName);
IClientProxy User(string userId); TClient User(string userId);
} }
} }

View File

@ -5,8 +5,12 @@ using System.Threading.Tasks;
namespace Microsoft.AspNetCore.SignalR namespace Microsoft.AspNetCore.SignalR
{ {
public interface IHubContext<THub> public interface IHubContext<THub, TClient>
{
IHubConnectionContext<TClient> Clients { get; }
}
public interface IHubContext<THub> : IHubContext<THub, IClientProxy>
{ {
IHubConnectionContext Clients { get; }
} }
} }