More refactoring

- Moved the IHubConnectionContext implementation out of HubEndPoint
- Added IHubContext to allow getting at the publish side of things without
 being in side the hub. The HubEndPoint now injects this as well.
- HubContext has the implementation of the IHubConnectionContext
- Moved ISignalRBuilder and SignalRBuilder into their own files
This commit is contained in:
David Fowler 2016-11-02 03:03:35 -07:00
parent f41bcb9b2d
commit 6af6db67f4
6 changed files with 87 additions and 35 deletions

View File

@ -11,25 +11,11 @@ namespace Microsoft.Extensions.DependencyInjection
public static ISignalRBuilder AddSignalR(this IServiceCollection services)
{
services.AddSingleton(typeof(HubLifetimeManager<>), typeof(DefaultHubLifetimeManager<>));
services.AddSingleton(typeof(IHubContext<>), typeof(HubContext<>));
services.AddSingleton(typeof(HubEndPoint<>), typeof(HubEndPoint<>));
services.AddSingleton(typeof(RpcEndpoint<>), typeof(RpcEndpoint<>));
return new SignalRBuilder(services);
}
}
public interface ISignalRBuilder
{
IServiceCollection Services { get; }
}
public class SignalRBuilder : ISignalRBuilder
{
public SignalRBuilder(IServiceCollection services)
{
Services = services;
}
public IServiceCollection Services { get; }
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.SignalR
{
public class HubContext<THub> : IHubContext<THub>, IHubConnectionContext
{
private readonly HubLifetimeManager<THub> _lifetimeManager;
private readonly AllClientProxy<THub> _all;
public HubContext(HubLifetimeManager<THub> lifetimeManager)
{
_lifetimeManager = lifetimeManager;
_all = new AllClientProxy<THub>(_lifetimeManager);
}
public IHubConnectionContext Clients => this;
public virtual IClientProxy All => _all;
public virtual IClientProxy Client(string connectionId)
{
return new SingleClientProxy<THub>(_lifetimeManager, connectionId);
}
public virtual IClientProxy Group(string groupName)
{
return new GroupProxy<THub>(_lifetimeManager, groupName);
}
public virtual IClientProxy User(string userId)
{
return new UserProxy<THub>(_lifetimeManager, userId);
}
}
}

View File

@ -6,36 +6,21 @@ using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.SignalR
{
public class HubEndPoint<THub> : RpcEndpoint<THub>, IHubConnectionContext where THub : Hub
public class HubEndPoint<THub> : RpcEndpoint<THub> where THub : Hub
{
private readonly AllClientProxy<THub> _all;
private readonly HubLifetimeManager<THub> _lifetimeManager;
private readonly IHubContext<THub> _hubContext;
public HubEndPoint(HubLifetimeManager<THub> lifetimeManager,
IHubContext<THub> hubContext,
InvocationAdapterRegistry registry,
ILoggerFactory loggerFactory,
IServiceScopeFactory serviceScopeFactory)
: base(registry, loggerFactory, serviceScopeFactory)
{
_lifetimeManager = lifetimeManager;
_all = new AllClientProxy<THub>(_lifetimeManager);
}
public virtual IClientProxy All => _all;
public virtual IClientProxy Client(string connectionId)
{
return new SingleClientProxy<THub>(_lifetimeManager, connectionId);
}
public virtual IClientProxy Group(string groupName)
{
return new GroupProxy<THub>(_lifetimeManager, groupName);
}
public virtual IClientProxy User(string userId)
{
return new UserProxy<THub>(_lifetimeManager, userId);
_hubContext = hubContext;
}
public override async Task OnConnected(Connection connection)
@ -73,7 +58,7 @@ namespace Microsoft.AspNetCore.SignalR
private void Initialize(Connection connection, THub hub)
{
hub.Clients = this;
hub.Clients = _hubContext.Clients;
hub.Context = new HubCallerContext(connection);
hub.Groups = new GroupManager<THub>(connection, _lifetimeManager);
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.SignalR
{
public interface IHubContext<THub>
{
IHubConnectionContext Clients { get; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.SignalR
{
public interface ISignalRBuilder
{
IServiceCollection Services { get; }
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.SignalR
{
public class SignalRBuilder : ISignalRBuilder
{
public SignalRBuilder(IServiceCollection services)
{
Services = services;
}
public IServiceCollection Services { get; }
}
}