Click here to Skip to main content
15,916,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am developing a chat application like facebook in MVC 4.

I am connecting to hub like following

JavaScript
var chat = $.connection.chatHub;

$.connection.transports.longPolling.supportsKeepAlive = function () {
    return false;
}
$.connection.hub.qs = "UserID=" + $('#hdfLoggedInUserID').val();
$.connection.hub.start().done(function () {
    //...
});
$.connection.hub.disconnected(function () {
    setTimeout(function () {
        $.connection.hub.start();
    }, 5000); // Restart connection after 5 seconds.
});


And my hub class is like following

C#
public override Task OnConnected()
    {
        var userID = Context.QueryString["UserID"];
        if (userID != null)
        {
            //set user online in database and save connection_id
            RefreshOnlineUsers(uId);
        }
        return base.OnConnected();
    }
    public override Task OnDisconnected(bool stopCalled)
    {
        var userID = Context.QueryString["UserID"];
        if (userID != null)
        {
            //set user offline in database
            RefreshOnlineUsers(uId);
        }
        return base.OnDisconnected(stopCalled);
    }

It's working fine when I am working with only one browser tab.

OnConnected calls when browser tab open and OnDisconnected calls when browser tab closed. So according to this when I open one tab then OnConnected calls and user's status(online) and connection_id update in database.

After that I open my application in new tab of same browser then OnConnected method calls again and user's new connection_id and status(online) update in database.

After that when I close new browser tab then OnDiconnected calls and user's status changes to offline in database. But user is still online in previous browser tab.

Due to this issue this user is offline according to database but it's online.

Q : How can I resolve this multiple browser tab issue?

Thanks.

What I have tried:

I am already done lot of R&D about it but can't find any proper solution.
Posted
Updated 2-Jan-17 1:15am

1 solution

I was only tracking the user whereas I need to track the connectionId also.
When the user has no active connectionIds then they will be offline other wise they will be online.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900