Add HubCallerContext to Hub
- Flow the connection id and user of the caller to the Hub
This commit is contained in:
parent
e59865a11d
commit
50e5827414
|
|
@ -2,6 +2,7 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Channels;
|
using Channels;
|
||||||
|
using Microsoft.AspNetCore.Sockets;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using SocketsSample.Hubs;
|
using SocketsSample.Hubs;
|
||||||
|
|
@ -28,12 +29,13 @@ namespace SocketsSample
|
||||||
return new SingleClientProxy(this, connectionId);
|
return new SingleClientProxy(this, connectionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Initialize(object endpoint)
|
protected override void Initialize(Connection connection, object endpoint)
|
||||||
{
|
{
|
||||||
var hub = (Hub)endpoint;
|
var hub = (Hub)endpoint;
|
||||||
hub.Clients = this;
|
hub.Clients = this;
|
||||||
|
hub.Context = new HubCallerContext(connection.ConnectionId, connection.User);
|
||||||
|
|
||||||
base.Initialize(endpoint);
|
base.Initialize(connection, endpoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void DiscoverEndpoints()
|
protected override void DiscoverEndpoints()
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,8 @@ namespace SocketsSample
|
||||||
{
|
{
|
||||||
public class RpcEndpoint : EndPoint
|
public class RpcEndpoint : EndPoint
|
||||||
{
|
{
|
||||||
private readonly Dictionary<string, Func<InvocationDescriptor, InvocationResultDescriptor>> _callbacks
|
private readonly Dictionary<string, Func<Connection, InvocationDescriptor, InvocationResultDescriptor>> _callbacks
|
||||||
= new Dictionary<string, Func<InvocationDescriptor, InvocationResultDescriptor>>(StringComparer.OrdinalIgnoreCase);
|
= new Dictionary<string, Func<Connection, InvocationDescriptor, 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 ILogger<RpcEndpoint> _logger;
|
private readonly ILogger<RpcEndpoint> _logger;
|
||||||
|
|
@ -71,10 +71,10 @@ namespace SocketsSample
|
||||||
}
|
}
|
||||||
|
|
||||||
InvocationResultDescriptor result;
|
InvocationResultDescriptor result;
|
||||||
Func<InvocationDescriptor, InvocationResultDescriptor> callback;
|
Func<Connection, InvocationDescriptor, InvocationResultDescriptor> callback;
|
||||||
if (_callbacks.TryGetValue(invocationDescriptor.Method, out callback))
|
if (_callbacks.TryGetValue(invocationDescriptor.Method, out callback))
|
||||||
{
|
{
|
||||||
result = callback(invocationDescriptor);
|
result = callback(connection, invocationDescriptor);
|
||||||
}
|
}
|
||||||
else
|
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);
|
_logger.LogDebug("RPC method '{methodName}' is bound", methodName);
|
||||||
}
|
}
|
||||||
|
|
||||||
_callbacks[methodName] = invocationDescriptor =>
|
_callbacks[methodName] = (connection, invocationDescriptor) =>
|
||||||
{
|
{
|
||||||
var invocationResult = new InvocationResultDescriptor();
|
var invocationResult = new InvocationResultDescriptor();
|
||||||
invocationResult.Id = invocationDescriptor.Id;
|
invocationResult.Id = invocationDescriptor.Id;
|
||||||
|
|
@ -125,7 +125,7 @@ namespace SocketsSample
|
||||||
{
|
{
|
||||||
object value = scope.ServiceProvider.GetService(type) ?? Activator.CreateInstance(type);
|
object value = scope.ServiceProvider.GetService(type) ?? Activator.CreateInstance(type);
|
||||||
|
|
||||||
Initialize(value);
|
Initialize(connection, value);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,14 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Security.Claims;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace SocketsSample.Hubs
|
namespace SocketsSample.Hubs
|
||||||
{
|
{
|
||||||
public class Hub
|
public class Hub
|
||||||
{
|
{
|
||||||
public IHubConnectionContext Clients { get; set; }
|
public IHubConnectionContext Clients { get; set; }
|
||||||
|
|
||||||
|
public HubCallerContext Context { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IHubConnectionContext
|
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>
|
/// <returns>A task that represents when the data has been sent to the client.</returns>
|
||||||
Task Invoke(string method, params object[] args);
|
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; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue