简体   繁体   中英

Multiple client calls after page refresh with SignalR

We have a application with multiple hubs using Signal R 1.1.3, that monitors currently logged on users. The hub periodically refreshes the user count with the following script

     $.extend(sr_users.client, {
    showCurrentUserCount: function (data) {
        console.log("Showing Current Users");
        $('#current-users').html(data);
    }
});

Which produces the following line

   [xx:xx:xx GMT+0000 (GMT Standard Time)] SignalR: Triggering client hub event 'showCurrentUserCount' on hub 'userhub'.

However when the browser is refreshed the above client hub event is called multiple times

  [15:40:20 GMT+0000 (GMT Standard Time)] SignalR: Triggering client hub event 'showCurrentUserCount' on hub 'userhub'.
  [15:40:20 GMT+0000 (GMT Standard Time)] SignalR: Triggering client hub event 'showCurrentUserCount' on hub 'userhub'.

How can I prevent this from happening

Thank you

Additional Code

    sr_users = $.connection.userhub;
$.connection.hub.logging = true;

    //update any ui when the hub starts
$.connection.hub.start()
    .done(function () {
        sr_users.server.getCurrentUsers()
        .done(function (users) {
            $('#current-users').html(users);
        });
    });

Sorted it! Doh! Put an event subscription on the Hub constructor. Bad idea

   public UserHub()
   {
      Connections.Instance.OnPurge += ConnectionsUpdate
   }

So each time the page was refreshed the event would be subscribed to. I have no removed the procedure completely and gave the connection Instance access to the HUbContext from where it can send out to all clients

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM