2015-03-31 70 views
0

我有一個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 
+0

你在項目中引用了'signalr/hubs'嗎? – 2015-03-31 08:21:33

回答

0

我覺得你的中心是確定指標不正確(僞代碼不是100%):

這就是您的集線器定義:

class NotificationsHub : Hub { 
    public void DisplayNotification(string msg){} 
} 
在sendNotification的方法使用

IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationsHub>(); 
context.Clients.All.displayNotification(msg); 

,或者使用一個單獨的線人類不是我已經找到了解決方案中心

+0

對不起,但沒有幫助,數據仍然不顯示。它仍然顯示舊數據... – MTplus 2015-03-31 07:52:37

1

,我有我的第2個引用jQuery的。 第一個被放置在head部分,是jquery-1.6.4.min,另一個放在body標籤(jQuery v1.11.2)的末尾。

刪除放置在body標籤(jQuery v1.11.2)末尾的標籤後,它的工作原理如下。