C# Webapplication: Why is session usercount (in global.Session_Start) different from hub usercount (in hub OnConnected)

42 views Asked by At

I want to display the number of currently active users. I first realized that in global.Session_Start

protected void Session_Start(object sender, EventArgs e)
{
    int i = 0;
    Application.Lock();
    i = objectTools.asInt(Application["TotalOnlineUsers"], 0);
    Application["TotalOnlineUsers"] = i + 1;
    Application.UnLock();
}
protected void Session_End(object sender, EventArgs e)
{
    int i = 0;
    Application.Lock();
    i = objectTools.asInt(Application["TotalOnlineUsers"], 0);
    Application["TotalOnlineUsers"] = i - 1;
    Application.UnLock();
}

Than I stumbled over signalr/hubs and found the idea to see the active users count in real time very sexy.

In the hub-code myHub.cs I have a list in the class "myHub": public static List Users = new List(); which maintains the current users with their Context.QueryString["clientId"]. This code I have seen somewhere in an example. Using the hubs events "OnConnected", "Reconnect" and "Disconnect" I maintain that list and send the number list.count to all clients.

On the aspx-side I catch document.ready with this code, which I found on Display Online Userlist using SignalR

$(document).ready(function () {
    //Signal goes here
    try {
        var userActivity = $.connection.RadioSignalHub;
        console.log("item: " + userActivity);
    
        userActivity.client.updateUsersOnlineCount = function (count) {
            // Add the message to the page. 
            $('#usersCount').text(count);
            //alert(count);
            console.log("Count: " + count);
        };

        $.connection.hub.start();
    }
    catch (e) {
        console.log(e.description);
    }
}

Thats works so far. But the number of users using signalr is significant smaller than the Session-Count from global.cs.

Does anybody here has a clue, what the difference comes from?

Thank you so much!

0

There are 0 answers