Learn something new and record it. Maybe someone just needs ~~~~~~.
Due to the need to record the current online users, Emmmm has not done ...
Originally wanted to use the form of a database, but think of such a simple function Baidu certainly has. Travel a wave of Baidu, some harvest ....
Although there are always so many articles to repeat ....
Probably is when the user logs in the session record the data, the foreground obtains the display. The following article feels fine.
Session Method Reference URL:
Https://www.cnblogs.com/taobox/p/4466187.html
See the article reminds me of the usefulness of the project to Signair, this can be realized chat room can certainly know the number of online users. Resolutely continue Baidu to get the following results ....
Reference URL:
https://www.red-gate.com/simple-talk/dotnet/asp-net/tracking-online-users-with-signalr/
Signair has the onconnected,onconnected,ondisconnected three methods, the concrete realization is as follows.
public class Chathub:hub
{
Current number of users
private static int _usercount = 0;
Storing user information
private static list<string> List_username = new list<string> ();
Link
public override Task onconnected ()
{
var username = Context.User.Identity.Name; Get the name of the user
if (!list_username.contains (UserName))
{
_usercount++;
List_username.add (UserName);
}
Else
{
List_username.add (UserName);
}
var context = globalhost.connectionmanager.gethubcontext<chathub> ();
Context. Clients.All.online (_usercount);//Push Global, can also be pushed to the specified user
Return base. Onconnected ();
}
reconnects
public override Task onconnected ()
{
var username = Context.User.Identity.Name;
if (!list_username.contains (UserName))
{
_usercount++;
List_username.add (UserName);
}
Else
{
List_username.add (UserName);
}
var context = globalhost.connectionmanager.gethubcontext<chathub> ();
Context. Clients.All.online (_usercount);
Return base. Onreconnected ();
}
Disconnect
public override Task ondisconnected (bool stopcalled)
{
var username = Context.User.Identity.Name;
var count = list_username.where (x = = = = UserName). Count ();
if (count = = 1)
{
_usercount--;
List_username.remove (UserName);
}
Else
{
List_username.remove (UserName);
}
var context = globalhost.connectionmanager.gethubcontext<chathub> ();
Context. Clients.All.online (_usercount);
Return base. Ondisconnected (stopcalled);
}
}
Js:
var chat = $.connection.chathub;
Chat.client.online = function (count) {
$ ("#online"). HTML (count); Label Assignment
}
$.connection.hub.start ();
This will give you a number of user links, and then I assign a value to the control inside the _layout.cshtml, and it's done.
There is a little lack of the above, you should be able to see me in the List_username collection there are repeated additions and deletions of the action.
This approach was implemented because it was not possible to confirm that the page was last closed (or I didn't find it).
. NET MVC uses SIGNALR to update the number of online users