.Net Core SignalR:从 IHubContext 发送给除调用者以外的用户(注入控制器)
.Net Core SignalR: Send to Users except caller from IHubContext (injected into controller)
为了仅在控制器中使用注入的 IHubContext 时识别当前用户,我存储了一个具有用户 ID 的组。
但是,我很难发送给其他人,因为我无法找到一种方法来找出要排除的连接 ID。
我的中心
public override Task OnConnectedAsync()
{
Groups.AddAsync(Context.ConnectionId, Context.User.Identity.Name);
return base.OnConnectedAsync();
}
在我的控制器方法中,我可以为该用户调用方法:
await _signalRHub.Clients.Group(User.Identity.Name).InvokeAsync("Send", User.Identity.Name + ": Message for you");
IHubContext.Clients.AllExcept 需要连接 ID 列表。如何获取已识别用户的连接 ID,以便只通知其他人?
正如@Pawel 所建议的那样,我现在正在对客户端进行重复数据删除,这是有效的(好吧,只要您的所有客户端都经过身份验证)。
private async Task Identification() => await Clients.Group(Context.User.Identity.Name).InvokeAsync("Identification", Context.User.Identity.Name);
public override async Task OnConnectedAsync()
{
await Groups.AddAsync(Context.ConnectionId, Context.User.Identity.Name);
await base.OnConnectedAsync();
await Identification();
}
附带的 JS(缩写):
var connection = new signalR.HubConnection("/theHub");
var myIdentification;
connection.on("Identification", userId => {
myIdentification = userId;
});
现在您可以使用 connection.on("something", callerIdentification)
等其他方法测试 callerIdentification == myIdentification
@Tester 的评论让我希望在通过 IHubContext 发送时会有更好的方法。
为了仅在控制器中使用注入的 IHubContext 时识别当前用户,我存储了一个具有用户 ID 的组。 但是,我很难发送给其他人,因为我无法找到一种方法来找出要排除的连接 ID。
我的中心
public override Task OnConnectedAsync()
{
Groups.AddAsync(Context.ConnectionId, Context.User.Identity.Name);
return base.OnConnectedAsync();
}
在我的控制器方法中,我可以为该用户调用方法:
await _signalRHub.Clients.Group(User.Identity.Name).InvokeAsync("Send", User.Identity.Name + ": Message for you");
IHubContext.Clients.AllExcept 需要连接 ID 列表。如何获取已识别用户的连接 ID,以便只通知其他人?
正如@Pawel 所建议的那样,我现在正在对客户端进行重复数据删除,这是有效的(好吧,只要您的所有客户端都经过身份验证)。
private async Task Identification() => await Clients.Group(Context.User.Identity.Name).InvokeAsync("Identification", Context.User.Identity.Name);
public override async Task OnConnectedAsync()
{
await Groups.AddAsync(Context.ConnectionId, Context.User.Identity.Name);
await base.OnConnectedAsync();
await Identification();
}
附带的 JS(缩写):
var connection = new signalR.HubConnection("/theHub");
var myIdentification;
connection.on("Identification", userId => {
myIdentification = userId;
});
现在您可以使用 connection.on("something", callerIdentification)
callerIdentification == myIdentification
@Tester 的评论让我希望在通过 IHubContext 发送时会有更好的方法。