2014-09-03 47 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); 
    }