SignalR 如何获取连接用户的声明?
SignalR how can I get access to claims from connected users?
在我的应用程序 .net core 中,我想通知用户其他用户所做的一些操作。
我的 SignalR 连接已获得授权,因此 SignalR 应该有权访问已连接的用户声明,他也有。
在那些声明中,用户有 ID,所以我在想也许我可以根据该 ID 以某种方式搜索用户,并将通知发送给特定用户,但我不知道如何在 mHubContext 中找到他,或者我应该以任何其他方式做到这一点?
也许我应该覆盖 OnConnected
方法并将他们的 ID 存储在一些静态 hashset/dictionary?
您可以使用以下方法来访问某些属性用户、连接等
[HubName("demoHub")]
public class DemoHub : Hub
{
public readonly static ConnectionMapping<string> connections =
new ConnectionMapping<string>();
public override async Task OnConnected()
{
string name = Context.User.Identity.Name;
connections.Add(name, Context.ConnectionId);
//await Groups.Add(Context.ConnectionId, Context.QueryString["group"]);
await base.OnConnected();
}
public override async Task OnDisconnected(bool stopCalled)
{
string name = Context.User.Identity.Name;
connections.Remove(name, Context.ConnectionId);
//await Groups.Remove(Context.ConnectionId, Context.QueryString["group"]);
await base.OnDisconnected(stopCalled);
}
public override async Task OnReconnected()
{
string name = Context.User.Identity.Name;
if (!connections.GetConnections(name).Contains(Context.ConnectionId))
{
connections.Add(name, Context.ConnectionId);
//await Groups.Add(Context.ConnectionId, Context.QueryString["group"]);
}
await base.OnReconnected();
}
}
更新: 如果需要,您可以使用以下方法或使用相同的逻辑创建类似的方法:
public Task JoinToGroup(string groupName)
{
return Groups.Add(Context.ConnectionId, groupName);
}
public Task LeaveFromGroup(string groupName)
{
return Groups.Remove(Context.ConnectionId, groupName);
}
public async Task AddToGroup(string connectionId, string groupName)
{
await Groups.Add(connectionId, groupName);
//await Clients.Group(groupName).ReceiveMessage("Send", $"{Context.ConnectionId} has joined the group {groupName}.");
}
public async Task RemoveFromGroup(string connectionId, string groupName)
{
await Groups.Remove(connectionId, groupName);
//await Clients.Group(groupName).ReceiveMessage("Send", $"{Context.ConnectionId} has left the group {groupName}.");
}
在我的应用程序 .net core 中,我想通知用户其他用户所做的一些操作。
我的 SignalR 连接已获得授权,因此 SignalR 应该有权访问已连接的用户声明,他也有。
在那些声明中,用户有 ID,所以我在想也许我可以根据该 ID 以某种方式搜索用户,并将通知发送给特定用户,但我不知道如何在 mHubContext 中找到他,或者我应该以任何其他方式做到这一点?
也许我应该覆盖 OnConnected
方法并将他们的 ID 存储在一些静态 hashset/dictionary?
您可以使用以下方法来访问某些属性用户、连接等
[HubName("demoHub")]
public class DemoHub : Hub
{
public readonly static ConnectionMapping<string> connections =
new ConnectionMapping<string>();
public override async Task OnConnected()
{
string name = Context.User.Identity.Name;
connections.Add(name, Context.ConnectionId);
//await Groups.Add(Context.ConnectionId, Context.QueryString["group"]);
await base.OnConnected();
}
public override async Task OnDisconnected(bool stopCalled)
{
string name = Context.User.Identity.Name;
connections.Remove(name, Context.ConnectionId);
//await Groups.Remove(Context.ConnectionId, Context.QueryString["group"]);
await base.OnDisconnected(stopCalled);
}
public override async Task OnReconnected()
{
string name = Context.User.Identity.Name;
if (!connections.GetConnections(name).Contains(Context.ConnectionId))
{
connections.Add(name, Context.ConnectionId);
//await Groups.Add(Context.ConnectionId, Context.QueryString["group"]);
}
await base.OnReconnected();
}
}
更新: 如果需要,您可以使用以下方法或使用相同的逻辑创建类似的方法:
public Task JoinToGroup(string groupName)
{
return Groups.Add(Context.ConnectionId, groupName);
}
public Task LeaveFromGroup(string groupName)
{
return Groups.Remove(Context.ConnectionId, groupName);
}
public async Task AddToGroup(string connectionId, string groupName)
{
await Groups.Add(connectionId, groupName);
//await Clients.Group(groupName).ReceiveMessage("Send", $"{Context.ConnectionId} has joined the group {groupName}.");
}
public async Task RemoveFromGroup(string connectionId, string groupName)
{
await Groups.Remove(connectionId, groupName);
//await Clients.Group(groupName).ReceiveMessage("Send", $"{Context.ConnectionId} has left the group {groupName}.");
}