我目前正在使用ASP.NET开发的网站。我使用SignalR从数据库发布实时更改。例如,我有一个表中有消息和NewMessageCount
。每次NewMessageCount
上升时,用户都会收到通知。然而,我的问题是,当用户点击通知时,它不会减少。例如,如果用户具有两个通知并点击Message
标签上它下降到0,而不是1,然后0减量更新查询
这里是关于用在Hub
[HubMethodName("removeNotifications")]
public string RemoveNotifications()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
using (SqlCommand command = new SqlCommand("UPDATE Messages SET [email protected]", connection))
{
command.Parameters.AddWithValue("@NewMessageCount", totalNewMessages);
command.Notification = null;
DataTable dt = new DataTable();
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
connection.Open();
var reader = command.ExecuteReader();
}
connection.Close();
}
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
return context.Clients.All.RemoveNotification(totalNewMessages);
}
而到了script
代码客户端是以下
<script type="text/javascript">
$(function() {
var notifications = $.connection.notificationHub;
notifications.client.recieveNotification = function (totalNewMessages) {
$('#spanNewMessages').text(totalNewMessages);
};
$.connection.hub.start(function() {
notifications.server.sendNotifications();
$('.Message').click(function() {
notifications.server.removeNotifications();
})
});
});
</script>
我可能会犯一个明显的错误,但我似乎无法弄清楚。在此先感谢您的帮助和支持。
凡totalNewMessages大干快上一个更新的图了这一点服务器端? – 2014-09-03 11:53:32
@AlexArt。他们在'NewMessagecount'列中得到更新 – Izzy 2014-09-03 11:54:50
也许你应该返回context.Clients.All.RecieveNotification(totalNewMessages); – 2014-09-03 11:56:00