diff --git a/App_Code/NotificacionesSQLWatcher.vb b/App_Code/NotificacionesSQLWatcher.vb new file mode 100644 index 000000000..327dbc656 --- /dev/null +++ b/App_Code/NotificacionesSQLWatcher.vb @@ -0,0 +1,73 @@ +Option Explicit On +Option Strict Off +Imports System.Data.SqlClient +Imports ADSNetLibrary.Helpers +Imports Microsoft.AspNet.SignalR + +Public Class NotificacionesSQLWatcher + Private _hubContext As IHubContext + Public rutadb As String + Public Sub New() + rutadb = ConfigurationManager.ConnectionStrings("Conexion").ConnectionString + ' Obtener el contexto del Hub de SignalR + _hubContext = GlobalHost.ConnectionManager.GetHubContext(Of Notificaciones)() + End Sub + + ' Iniciar el proceso de escucha de cambios en SQL + Public Sub StartWatching() + ' Detener y reiniciar SqlDependency para evitar múltiples suscripciones + SqlDependency.Stop(rutadb) + SqlDependency.Start(rutadb) + + ' Iniciar el proceso de escucha + RegisterNotification() + End Sub + + ' Método para registrar notificaciones SQL + Private Sub RegisterNotification() + Using connection As New SqlConnection(rutadb) + ' Consulta para detectar cambios recientes + Dim command As New SqlCommand( + "SELECT CveUsuario, Notificaciones, Fecha FROM NotificacionesUsuarios", connection) + + ' Asociar dependencia SQL a la consulta + Dim dependency As New SqlDependency(command) + AddHandler dependency.OnChange, AddressOf OnNotificationChange + + connection.Open() + command.ExecuteReader() + End Using + End Sub + + ' Método ejecutado cuando ocurre un cambio en la tabla + Private Sub OnNotificationChange(sender As Object, e As SqlNotificationEventArgs) + ' Volver a escuchar cambios inmediatamente + RegisterNotification() + + ' Enviar notificaciones con los registros actualizados + SendNotifications() + End Sub + + ' Método para enviar notificaciones solo de los registros actualizados + Private Sub SendNotifications() + Using connection As New SqlConnection(rutadb) + Dim query As String = "SELECT CveUsuario, Notificaciones, Fecha FROM NotificacionesUsuarios WHERE Fecha > DATEADD(MINUTE, -1, GETDATE())" + Dim command As New SqlCommand(query, connection) + + connection.Open() + Dim reader As SqlDataReader = command.ExecuteReader() + + ' Iterar sobre los registros actualizados y enviar notificaciones + While reader.Read() + Dim cveUsuario As Integer = reader("CveUsuario").ToString().ToEntero() + Dim notificacion As String = reader("Notificaciones").ToString() + Dim fecha As String = reader("Fecha").ToString() + + ' Enviar al cliente usando SignalR + _hubContext.Clients.Client(Notificaciones.UsuariosConexiones(cveUsuario)).ContadorCampana(notificacion) + '_hubContext.Clients.All.broadcastMessage(cveUsuario, notificacion, fecha) + End While + End Using + End Sub +End Class + diff --git a/Global.asax b/Global.asax index b51b33bee..a07e05b26 100644 --- a/Global.asax +++ b/Global.asax @@ -48,6 +48,11 @@ GlobalConfiguration.Configure(AddressOf WebApiConfig.Register) GlobalConfiguration.Configuration.EnsureInitialized() + + + Dim watcher As New NotificacionesSQLWatcher() + watcher.StartWatching() + End Sub Sub Application_End(sender As Object, e As EventArgs) diff --git a/Scripts/Notificaciones.js b/Scripts/Notificaciones.js index faef712ec..14d6911c3 100644 --- a/Scripts/Notificaciones.js +++ b/Scripts/Notificaciones.js @@ -94,10 +94,17 @@ var NotificacionesADS = function () { } } }; + var execContadorCampana = function (contadormessage) { + $(".SigCampananoti").text(contadormessage); + $(".SigCampananoti").addClass('badge-danger'); + $(".SigCampananoti").removeClass('badge-default'); + //console.log('(SignalR) contadormessage \'' + contadormessage); + } var ContadorCampana = function (contadormessage) { - console.log('(SignalR) ContadorCampana \'' + contadormessage); - localStorage.setItem("ContadorCampana", ""); + //console.log('(SignalR) ContadorCampana \'' + contadormessage); + localStorage.setItem("ContadorCampana", ''); localStorage.setItem("ContadorCampana", contadormessage); + execContadorCampana(contadormessage); } //-------------------------------------------------------------------- EndEventos ---------------------------------------------------------------------- function closeopenConexion() { @@ -183,7 +190,7 @@ var NotificacionesADS = function () { case 'ContadorCampana': try { - console.log('(SignalR) contadormessage \'' + evt.newValue); + execContadorCampana(evt.newValue); } catch (e) { console.error('(SignalR) contadormessage \''+e); diff --git a/Site.master b/Site.master index 89641e5d6..7fd217389 100644 --- a/Site.master +++ b/Site.master @@ -136,7 +136,7 @@