Sqldep_onchange with Signalr does not work

Asked

Viewed 91 times

0

Follows code:

Notificationhub : Hub

private readonly static ConnectionMapping<string> _connections = new ConnectionMapping<string>();

public void SendNotification(string who)
{
    foreach (var connectionId in _connections.GetConnections(who))
    {
        var result = Clients.Client(connectionId);

        Clients.Client(connectionId).addNotification(who);
    }
}

Components.Cs:

void SqlDep_OnChange(object sender, SqlNotificationEventArgs e)
{

    //from here we will send notification message to client
    NotificationHub notHub = new NotificationHub();
    notHub.SendNotification(Who);

    //...
}

_Layout:

$(function () {
    var connection = $.connection.notificationHub;

    //signalr method for push server message to client
    connection.client.addNotification = function (who) {
        //send notification here
        console.info("Send Notification")
    };

    // Start hub
    $.connection.hub.start().done(function () {
        console.log("SignalR Started")
    });
});

In class Huband on the line var result = Clients.Client(connectionId); is returning some fields as null.

Follow picture: https://postimg.org/image/wkgocwc5d/

The following code works with the function click jquery:

 $(function () {
        var connection = $.connection.notificationHub;

        //signalr method for push server message to client
        connection.client.addNotification = function (who) {
            //send notification here
            console.info("Send Notification")
        };

        // Start hub
        $.connection.hub.start().done(function () {
            $("#testeclick").click(function () {
                connection.server.sendNotification("User15248");
            });
            console.log("SignalR Started")
        });
    });

Follow the result after the function click jquery: https://postimg.org/image/p8iwdzaaf/

The function click works perfectly. I just can’t make it work with SqlDep_OnChange when making database changes.

The idea is: After doing an "update" in the database, show a notification to client with signalr.

Some solution ?


UPDATE: (Almost solved problem)

Notificationhub : Hub

public static void SendNotification(string who)
        {
            IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
            foreach (var connectionId in _connections.GetConnections(who))
            {
                var result = context.Clients.Client(connectionId);
                if (result != null)
                {
                    result.addNotification(who);
                }
            }
        }

Components.Cs:

void SqlDep_OnChange(object sender, SqlNotificationEventArgs e)
{

    //from here we will send notification message to client
    NotificationHub.SendNotification("User1586");
    //...
}

Problem almost solved, if you have 50 users, it sends the notification 50 times to user "User1586". If you have 1000 users, send 1000 times the notification.

With that line problem: NotificationHub.SendNotification("User1586");(goes by several times)

  • Which fields, specifically are null?

  • @jbueno Connection, Hubname and Invoker. Or if you prefer image: https://postimg.org/image/wkgocwc5d/

  • 1

    It seems that _connections.GetConnections(who) is returning all connections (from all users). Or, your javascript there is running numerous times for the same client, thus creating multiple connections to the same user.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.