This commit is contained in:
2025-01-02 10:16:26 -07:00
4 changed files with 89 additions and 4 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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);

View File

@@ -136,7 +136,7 @@
<li class="dropdown dropdown-extended dropdown-notification" id="header_notification_bar" runat="server">
<a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-close-others="true">
<i class="icon-bell"></i>
<span class='badge <%=If(countNotificaciones > 0, "badge-danger", "badge-default") %>'><%= countNotificaciones %> </span>
<span class='SigCampananoti badge <%=If(countNotificaciones > 0, "badge-danger", "badge-default") %>'><%= countNotificaciones %> </span>
</a>
<ul class="dropdown-menu">
<li class="external">