我有一個asp.net頁面,使用SQLDependency Asp.Net SignalR 2自動顯示數據庫中的更改。我的問題是,即使調試時它們是正確的,我也看不到頁面上的更改。如果我更新數據庫,頁面仍顯示舊數據,但我的中心獲取新數據。但沒有顯示。我得到了工作的唯一的事情就是通過這樣的計時器重新加載頁面..SignalR 2不更新
<script>
function cache_clear() {
window.location.reload(true);
}
$(document).ready(function() {
var count = 60;
setInterval(function() {
$("b[id=show-time]").html(count--);
if (count == 0) cache_clear();
}, 1000)});
</script>
但是,這也使的SqlDependency多餘的整體思路。任何人都可以給我提示我需要做些什麼才能使更新的數據顯示在頁面上?
<script>
$(function() {
var notify = $.connection.notificationsHub;
notify.client.displayNotification = function (msg) {
$("#newData").html(msg);
$("#LitDiv").hide();
};
$.connection.hub.start();
});
</script>
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Info == SqlNotificationInfo.Insert || e.Info == SqlNotificationInfo.Delete || e.Info == SqlNotificationInfo.Update)
{ SendNotifications(); }
}
public void NotfiyAllClients(string msg)
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationsHub>();
// I can see that my database changes are made here, but it does not display that on the page unless I reload the page
context.Clients.All.displayNotification(msg);
}
而且sendNotification的方法使用該...
var nHub = new NotificationsHub();
nHub.NotfiyAllClients(message);
編輯:如果我調出控制檯我得到這個錯誤..
JavaScript runtime error: Unable to get property 'notificationsHub' of undefined or null reference
你在項目中引用了'signalr/hubs'嗎? – 2015-03-31 08:21:33