SignalR 向特定客户端发送通知它不起作用
SignalR sending notification to a specific client it's not working
使用 SignalR 向特定用户发送通知时无法正常工作。
我将连接 ID 存储在 table 中,当应该发送通知时,我从数据库中获取了接收者的连接 ID,但他没有得到任何信息。我的代码有什么问题?
// get the connectionId of the receiver
if (_db.UserConnectionid != null)
{
var userConn = await _db.UserConnectionid.Where(x => x.UserId == receiver).Select(x => x.ConnectionId).FirstOrDefaultAsync();
//if the receiver is online
if (userConn != null)
{
await Clients.Client(userConn).SendAsync("RecieveMessage", message);
}
}
I'm storing connection IDs in a table and when the notification should be sent I get the connection ID for the receiver from the DB but he doesn't get anything. What is wrong with my code?
首先请注意,一个用户可能有多个连接ID,要解决这个问题,您可以尝试调试代码并确保您从数据库中检索到的连接ID与当前连接的连接ID相同用户。
此外,要向特定用户发送消息,您可以尝试获取特定user/receiver的所有存储的连接ID,然后通过指定的connectionIds发送消息,如下所示。
var ConnectionIds = _db.UserConnectionid.Where(x => x.UserId == receiver).Select(x => x.ConnectionId).ToList();
if (ConnectionIds.Count > 0)
{
await Clients.Clients(ConnectionIds).SendAsync("RecieveMessage", message);
}
如果您使用默认的用户声明机制进行授权,您可能可以查看此机制:
https://docs.microsoft.com/en-us/aspnet/core/signalr/authn-and-authz?view=aspnetcore-5.0#use-claims-to-customize-identity-handling
如果您将提供 IUserIdProvider 服务或将 userId 映射到 ClaimTypes.NameIdentifier 声明,您将能够使用此方法按字符串化的用户 ID 过滤您的 SignalR 客户端:
https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.signalr.ihubclients-1.user?view=aspnetcore-5.0#Microsoft_AspNetCore_SignalR_IHubClients_1_User_System_String_
像这样:
await Clients.User(receiver.ToString()).SendAsync("RecieveMessage", message);
按照文章中的建议,不要存储 Id。
使用 SignalR 向特定用户发送通知时无法正常工作。
我将连接 ID 存储在 table 中,当应该发送通知时,我从数据库中获取了接收者的连接 ID,但他没有得到任何信息。我的代码有什么问题?
// get the connectionId of the receiver
if (_db.UserConnectionid != null)
{
var userConn = await _db.UserConnectionid.Where(x => x.UserId == receiver).Select(x => x.ConnectionId).FirstOrDefaultAsync();
//if the receiver is online
if (userConn != null)
{
await Clients.Client(userConn).SendAsync("RecieveMessage", message);
}
}
I'm storing connection IDs in a table and when the notification should be sent I get the connection ID for the receiver from the DB but he doesn't get anything. What is wrong with my code?
首先请注意,一个用户可能有多个连接ID,要解决这个问题,您可以尝试调试代码并确保您从数据库中检索到的连接ID与当前连接的连接ID相同用户。
此外,要向特定用户发送消息,您可以尝试获取特定user/receiver的所有存储的连接ID,然后通过指定的connectionIds发送消息,如下所示。
var ConnectionIds = _db.UserConnectionid.Where(x => x.UserId == receiver).Select(x => x.ConnectionId).ToList();
if (ConnectionIds.Count > 0)
{
await Clients.Clients(ConnectionIds).SendAsync("RecieveMessage", message);
}
如果您使用默认的用户声明机制进行授权,您可能可以查看此机制: https://docs.microsoft.com/en-us/aspnet/core/signalr/authn-and-authz?view=aspnetcore-5.0#use-claims-to-customize-identity-handling
如果您将提供 IUserIdProvider 服务或将 userId 映射到 ClaimTypes.NameIdentifier 声明,您将能够使用此方法按字符串化的用户 ID 过滤您的 SignalR 客户端: https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.signalr.ihubclients-1.user?view=aspnetcore-5.0#Microsoft_AspNetCore_SignalR_IHubClients_1_User_System_String_
像这样:
await Clients.User(receiver.ToString()).SendAsync("RecieveMessage", message);
按照文章中的建议,不要存储 Id。