客户端如何授权向用户发送消息?
控制器发送。
控制器发送。
hubContext.Clients.User(User.Identity.Name).SendAsync();
目前消息尚未发送。我需要在OnConnection()中添加什么内容吗?或者SignalR是否具有用于ConnectionId和User.Identity.Name的现成映射机制?
目前这是我的实现方式,但我觉得不太正确。问题是如何使用相同的标准工具?
public static class HubConnections
{
public static Dictionary<string, List<string>> Users = new Dictionary<string, List<string>>();
public static List<string> GetUserId(string name)
{
return Users[name];
}
}
public class GameHub : Hub
{
public override Task OnConnectedAsync()
{
if (Context.User.Identity.IsAuthenticated
&& HubConnections.Users.ContainsKey(Context.User.Identity.Name)
&& !HubConnections.Users[Context.User.Identity.Name].Contains(Context.ConnectionId))
HubConnections.Users[Context.User.Identity.Name].Add(Context.ConnectionId);
else
HubConnections.Users.Add(Context.User.Identity.Name, new List<string> { Context.ConnectionId });
return base.OnConnectedAsync();
}
public override Task OnDisconnectedAsync(Exception exception)
{
if (Context.User.Identity.IsAuthenticated) HubConnections.Users.Remove(Context.User.Identity.Name);
return base.OnDisconnectedAsync(exception);
}
}
正如我之前所说的那样,我尝试过这样做,但它并没有起作用。
hubContext.Clients.User(User.Identity.Name).SendAsync();