Example #1
0
        void sqlDep_OnChange(object sender, SqlNotificationEventArgs e)
        {
            //or you can also check => if (e.Info == SqlNotificationInfo.Insert) , if you want notification only for inserted record
            if (e.Type == SqlNotificationType.Change)
            {
                //from here we will send notification message to client
                var notificationHub = GlobalHost.ConnectionManager.GetHubContext <NotificationHub>();

                //Get Connected users
                NotificationHub             notificationHub2     = new NotificationHub();
                Dictionary <string, string> lstAllConnectedUsers = notificationHub2.GetLstAllConnectedUsers();

                SqlDependency sqlDep = sender as SqlDependency;
                sqlDep.OnChange -= sqlDep_OnChange;


                List <string> clientList = new List <string>();
                foreach (var item in dBChangeNotificationModelList)
                {
                    foreach (KeyValuePair <string, string> entry in lstAllConnectedUsers)
                    {
                        if (item.UserId == Convert.ToInt32(entry.Key))
                        {
                            clientList.Add(entry.Value);
                        }
                    }
                }
                clientList.Distinct();

                notificationHub.Clients.All.test(dBChangeNotificationModelList.Count, lstAllConnectedUsers.Count, clientList.Count);
                //notificationHub.Clients.All.test1(1, 2, 3);
                //notificationHub.Clients.Clients(clientList).notify("added");

                //notificationHub.Clients.All.notify("added");



                //re-register notification
                RegisterNotification(DateTime.Now);
            }
        }