简体   繁体   中英

Signalr doesn't connect to hubs on firefox and chrome

I have a basic MVC application which is integrated with signal R. I'm tracking online users using this application.

Following is my Hub class

public class UserHub : Hub { static long counter = 0;

public override Task OnConnected()
{
    //Update Count when User is connected
    counter = counter+1;
    Clients.All.UpdateCount(counter);
    return base.OnConnected();
}

public override Task OnDisconnected(bool stopCalled)
{

    counter = counter-1;
    Clients.All.UpdateCount(counter);
    return base.OnDisconnected(stopCalled);

}

}

Following is my javascript code

        $.connection.hub.logging = true;
        //setup hubs
        var userHub = $.connection.userHub;
        $.connection.hub.start().done(function () {

        });

        //function to recieve data from server
        userHub.client.UpdateCount = function (count) {
            $('#counter').text(count);
        }

I have registered the in startup.cs class as well.

The problem is signalr Is throwing an error "SignalR: No hubs have been subscribed to. " When I refresh the page like 5 times, then the signal r is connected to hub and data is getting pulled.

Did anyone face this issue?

Any help is appreciated.

Thank you

Start the connection after you're subscribing to the client method, like so:

var cHub = $.connection.userHub;
cHub.client.UpdateCount = function (count) {
    $('#counter').text(count);
}

$.connection.hub.start();

I was able to resolve the issue by setting a timeout. Following is my code.

var cHub = $.connection.userHub;
            setTimeout(function () {
                $.connection.hub.start();
            },
                1000);
            //function to recieve data from server
            cHub.client.UpdateCount = function (count) {
                $('#counter').text(count);
            }

            $.connection.hub.logging = true;

I still was not able to figure out why the firefox is doing that.

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