<script type="text/javascript" src="Scripts/jquery-1.9.1.min.js"></script> <script src="Scripts/jquery-jtemplates.js" type="text/javascript"></script> <!--Reference the SignalR library. --> <script src="/Scripts/jquery.signalR-2.2.0.js"></script> <script src="/Scripts/jquery.signalR-2.2.0.min.js"></script> <!--Reference the autogenerated SignalR hub script. --> <script src="/signalr/hubs"></script> <script type="text/javascript"> $(function () { var notify = $.connection.notificationsHub; notify.client.displayNotification = function (msg) { alert(msg); }; $.connection.hub.start(); }); </script>
<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=.;Initial Catalog=TestDB;Integrated Security=True" providerName="System.Data.EntityClient" /> </connectionStrings> and this is the page public partial class Test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SendNotifications(); } List<Model.Suggestion> result; public List<Model.Suggestion> SendNotifications() { string conStr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; using (SqlConnection connection = new SqlConnection(conStr)) { string query = @"select top 5 [sgtID] , [Name] , [Phone] , [Gender] , [Age] ,[AnswerPerc] , [AnswerRank] , [date] , [Notes] from [dbo].[Suggestion] order by date desc"; using (SqlCommand command = new SqlCommand(query, connection)) { command.Notification = null; SqlDependency dependency = new SqlDependency(command); dependency.OnChange += new OnChangeEventHandler(dependency_OnChange); connection.Open(); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { reader.Read(); //result = reader[0].ToString(); } } } NotificationsHub nHub = new NotificationsHub(); //nHub.NotifyAllClients(message); nHub.NotfiyAllClients(result); return result; } private void dependency_OnChange(object sender, SqlNotificationEventArgs e) { if (e.Type == SqlNotificationType.Change) { SendNotifications(); } } } and this is the hub Class namespace TestSignalR.Hubs { public class NotificationsHub : Hub { private static string conString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString(); public void NotfiyAllClients(List<Model.Suggestion> sgt) { IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationsHub>(); context.Clients.All.displayNotification(sgt); } } } Here is the global class: string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; protected void Application_Start(object sender, EventArgs e) { SqlDependency.Start(connString); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)