2014-09-03 51 views
0

我目前正在使用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> 

我可能會犯一個明顯的錯誤,但我似乎無法弄清楚。在此先感謝您的幫助和支持。

+0

凡totalNewMessages大幹快上一個更新的圖了這一點服務器端? – 2014-09-03 11:53:32

+0

@AlexArt。他們在'NewMessagecount'列中得到更新 – Izzy 2014-09-03 11:54:50

+0

也許你應該返回context.Clients.All.RecieveNotification(totalNewMessages); – 2014-09-03 11:56:00

回答

0

我設法通過簡單地執行查詢到以下

string query = "UPDATE Messages SET NewMessageCount=NewMessageCount-1 WHERE [email protected] AND NewMessageCount > 0"; 
0

試試這個:

$(function() { 
     var notifications = $.connection.notificationHub; 
     var newMessages; 
     notifications.client.recieveNotification = function (totalNewMessages) { 
      newMessages = parseInt(totalNewMessages); 
      $('#spanNewMessages').text(totalNewMessages); 
     }; 
     $.connection.hub.start(function() { 
      notifications.server.sendNotifications(); 
      $('.Message').click(function() { 
       newMessages--; 
       notifications.server.removeNotifications(newMessages); 
     }) 
     }); 
}); 

服務器:

[HubMethodName("removeNotifications")] 
    public string RemoveNotifications(int newMessages) 
    { 
     if(newMessages < 0) 
     { 
      return context.Clients.All.RecieveNotification(0); 
     } 
     using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)) 
     { 
      using (SqlCommand command = new SqlCommand("UPDATE Messages SET [email protected]", connection)) 
       { 
        command.Parameters.AddWithValue("@NewMessageCount", newMessages); 
        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.RecieveNotification(newMessages); 
    }