. NET MVC uses SIGNALR to update the number of online users

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.