Added typed HubContext
This commit is contained in:
parent
bee9fcb0d8
commit
a7fb243501
|
|
@ -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<THub, T> : IHubContext<THub, T>, IHubClients<T>
|
||||||
|
where THub : Hub<T>
|
||||||
|
where T : class
|
||||||
|
{
|
||||||
|
private readonly HubLifetimeManager<THub> _lifetimeManager;
|
||||||
|
|
||||||
|
public HubContext(HubLifetimeManager<THub> lifetimeManager)
|
||||||
|
{
|
||||||
|
_lifetimeManager = lifetimeManager;
|
||||||
|
All = TypedClientBuilder<T>.Build(new AllClientProxy<THub>(_lifetimeManager));
|
||||||
|
Groups = new GroupManager<THub>(lifetimeManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IHubClients<T> Clients => this;
|
||||||
|
|
||||||
|
public virtual T All { get; }
|
||||||
|
|
||||||
|
public virtual IGroupManager Groups { get; }
|
||||||
|
|
||||||
|
public T AllExcept(IReadOnlyList<string> excludedIds)
|
||||||
|
{
|
||||||
|
return TypedClientBuilder<T>.Build(new AllClientsExceptProxy<THub>(_lifetimeManager, excludedIds));
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual T Client(string connectionId)
|
||||||
|
{
|
||||||
|
return TypedClientBuilder<T>.Build(new SingleClientProxy<THub>(_lifetimeManager, connectionId));
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual T Group(string groupName)
|
||||||
|
{
|
||||||
|
return TypedClientBuilder<T>.Build(new GroupProxy<THub>(_lifetimeManager, groupName));
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual T User(string userId)
|
||||||
|
{
|
||||||
|
return TypedClientBuilder<T>.Build(new UserProxy<THub>(_lifetimeManager, userId));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<THub, T>
|
||||||
|
where THub : Hub<T>
|
||||||
|
where T : class
|
||||||
|
{
|
||||||
|
IHubClients<T> Clients { get; }
|
||||||
|
|
||||||
|
IGroupManager Groups { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -13,6 +13,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
services.AddSingleton(typeof(HubLifetimeManager<>), typeof(DefaultHubLifetimeManager<>));
|
services.AddSingleton(typeof(HubLifetimeManager<>), typeof(DefaultHubLifetimeManager<>));
|
||||||
services.AddSingleton(typeof(IHubProtocolResolver), typeof(DefaultHubProtocolResolver));
|
services.AddSingleton(typeof(IHubProtocolResolver), typeof(DefaultHubProtocolResolver));
|
||||||
services.AddSingleton(typeof(IHubContext<>), typeof(HubContext<>));
|
services.AddSingleton(typeof(IHubContext<>), typeof(HubContext<>));
|
||||||
|
services.AddSingleton(typeof(IHubContext<,>), typeof(HubContext<,>));
|
||||||
services.AddSingleton(typeof(HubEndPoint<>), typeof(HubEndPoint<>));
|
services.AddSingleton(typeof(HubEndPoint<>), typeof(HubEndPoint<>));
|
||||||
services.AddScoped(typeof(IHubActivator<>), typeof(DefaultHubActivator<>));
|
services.AddScoped(typeof(IHubActivator<>), typeof(DefaultHubActivator<>));
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ using Moq;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Serialization;
|
using Newtonsoft.Json.Serialization;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.SignalR.Tests
|
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<IHubContext<SimpleHub>>();
|
||||||
|
await context.Clients.All.InvokeAsync("Send", "test");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CanLoadTypedHubContext()
|
||||||
|
{
|
||||||
|
var serviceProvider = CreateServiceProvider();
|
||||||
|
var context = serviceProvider.GetRequiredService<IHubContext<SimpleTypedHub, ITypedHubClient>>();
|
||||||
|
await context.Clients.All.Send("test");
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task LifetimeManagerOnDisconnectedAsyncCalledIfLifetimeManagerOnConnectedAsyncThrows()
|
public async Task LifetimeManagerOnDisconnectedAsyncCalledIfLifetimeManagerOnConnectedAsyncThrows()
|
||||||
{
|
{
|
||||||
|
|
@ -1579,5 +1596,19 @@ namespace Microsoft.AspNetCore.SignalR.Tests
|
||||||
await base.OnConnectedAsync();
|
await base.OnConnectedAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface ITypedHubClient
|
||||||
|
{
|
||||||
|
Task Send(string message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class SimpleTypedHub : Hub<ITypedHubClient>
|
||||||
|
{
|
||||||
|
public override async Task OnConnectedAsync()
|
||||||
|
{
|
||||||
|
await Clients.All.Send($"{Context.ConnectionId} joined");
|
||||||
|
await base.OnConnectedAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue