Add HubCallerContext to Hub

- Flow the connection id and user of the caller to the Hub
This commit is contained in:
David Fowler 2016-10-28 10:25:38 -07:00
parent e59865a11d
commit 50e5827414
3 changed files with 27 additions and 10 deletions

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Channels;
using Microsoft.AspNetCore.Sockets;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SocketsSample.Hubs;
@ -28,12 +29,13 @@ namespace SocketsSample
return new SingleClientProxy(this, connectionId);
}
protected override void Initialize(object endpoint)
protected override void Initialize(Connection connection, object endpoint)
{
var hub = (Hub)endpoint;
hub.Clients = this;
hub.Context = new HubCallerContext(connection.ConnectionId, connection.User);
base.Initialize(endpoint);
base.Initialize(connection, endpoint);
}
protected override void DiscoverEndpoints()

View File

@ -16,8 +16,8 @@ namespace SocketsSample
{
public class RpcEndpoint : EndPoint
{
private readonly Dictionary<string, Func<InvocationDescriptor, InvocationResultDescriptor>> _callbacks
= new Dictionary<string, Func<InvocationDescriptor, InvocationResultDescriptor>>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, Func<Connection, InvocationDescriptor, InvocationResultDescriptor>> _callbacks
= new Dictionary<string, Func<Connection, InvocationDescriptor, InvocationResultDescriptor>>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, Type[]> _paramTypes = new Dictionary<string, Type[]>();
private readonly ILogger<RpcEndpoint> _logger;
@ -71,10 +71,10 @@ namespace SocketsSample
}
InvocationResultDescriptor result;
Func<InvocationDescriptor, InvocationResultDescriptor> callback;
Func<Connection, InvocationDescriptor, InvocationResultDescriptor> callback;
if (_callbacks.TryGetValue(invocationDescriptor.Method, out callback))
{
result = callback(invocationDescriptor);
result = callback(connection, invocationDescriptor);
}
else
{
@ -90,7 +90,7 @@ namespace SocketsSample
}
}
protected virtual void Initialize(object endpoint)
protected virtual void Initialize(Connection connection, object endpoint)
{
}
@ -113,7 +113,7 @@ namespace SocketsSample
_logger.LogDebug("RPC method '{methodName}' is bound", methodName);
}
_callbacks[methodName] = invocationDescriptor =>
_callbacks[methodName] = (connection, invocationDescriptor) =>
{
var invocationResult = new InvocationResultDescriptor();
invocationResult.Id = invocationDescriptor.Id;
@ -125,7 +125,7 @@ namespace SocketsSample
{
object value = scope.ServiceProvider.GetService(type) ?? Activator.CreateInstance(type);
Initialize(value);
Initialize(connection, value);
try
{

View File

@ -1,12 +1,14 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
namespace SocketsSample.Hubs
{
public class Hub
{
public IHubConnectionContext Clients { get; set; }
public HubCallerContext Context { get; set; }
}
public interface IHubConnectionContext
@ -26,4 +28,17 @@ namespace SocketsSample.Hubs
/// <returns>A task that represents when the data has been sent to the client.</returns>
Task Invoke(string method, params object[] args);
}
public class HubCallerContext
{
public HubCallerContext(string connectionId, ClaimsPrincipal user)
{
ConnectionId = connectionId;
User = user;
}
public ClaimsPrincipal User { get; }
public string ConnectionId { get; }
}
}