显示基于登录用户的新消息计数
本文关键字:用户 新消息 登录 于登录 显示 | 更新日期: 2023-09-27 18:24:20
目前正在一个用asp和c#开发的网站上工作。每次在数据库中收到新消息时,我都会使用signaler发送新的通知。然而,我现在遇到的问题是,如果说User1有一条新消息,而User2则有两条新消息。它们都会显示给用户。因此,User1将有一个新消息,User2也将有一条新消息。我正在使用下面的代码
[HubMethodName("Notifications")]
public string SendNotifications()
{
string userName = HttpContext.Current.User.Identity.Name;
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
string query = "SELECT NewMessageCount FROM Messages WHERE UserName=@UserName";
using (SqlCommand command = new SqlCommand(query, connection))
{
SqlParameter para2 = new SqlParameter(@"UserName", userName);
command.Parameters.Add(para2);
command.Notification = null;
DataTable dt = new DataTable();
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
dt.Load(reader);
if (dt.Rows.Count > 0)
{
totalNewMessages = Int16.Parse(dt.Rows[0]["NewMessageCount"].ToString());
}
}
}
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
return context.Clients.All.RecieveNotification(totalNewMessages);
}
提前感谢您的帮助。
您应该使用Clients.User(userid)
context.Clients.User(userName).RecieveNotification(totalNewMessages);
是的,因为您正在向所有人广播。您尚未指定特定的连接。有关如何指定特定客户端/组的信息,请查看以下内容。选择哪些客户端将接收RPC