Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc who is online

Tags:

asp.net-mvc

Greetings, can someone give me some advices or links that will help me to implement to following scenario. Page will be written in asp.net mvc. Authorization is going to be implemented by Memberships. The scenario is as follows:

User1 has just logged in. After a while, User2 attempts to login with success. Then user1 should be notified that User2 has just logged in. Additionally User2 should be notified that User1 is online.

How can I achieve something like that? It should also be possible for these users to write messages to each other. (chat like).

like image 293
niao Avatar asked May 24 '10 23:05

niao


People also ask

Is ASP.NET MVC still used?

It is no longer in active development. It is open-source software, apart from the ASP.NET Web Forms component, which is proprietary. ASP.NET Core has since been released, which unified ASP.NET, ASP.NET MVC, ASP.NET Web API, and ASP.NET Web Pages (a platform using only Razor pages).

Is MVC only for web?

No, it applies even for standalone applications. Example Java Swing follows MVC.


3 Answers

There are methods to do this in the asp.net membership providers, specifically, IsUserOnline() and something like CountUsersOnline(). The only problem with these methods is that they are really lame. They depend on the membership provider's LastActivityDate() and a window you can set in web.config. In other words, the user is considered online if his last encounter with the membership provider plus the time window in web.config has not expired.

We took this scenairo and made it work for us by setting up a Comet server, and pinging the Web server every ten minutes. When the Web server is pinged, it updates the LastActivityDate of the membership provider.

We set the activity window to 12 minutes, as well as the Session timer. This allows us to determine who is online to an accuracy of aproximately ten minutes.


Here is the line in Web.config:

    <membership userIsOnlineTimeWindow="12">

Here is jQuery Comet server:

  function getData() { 
    $.getJSON("/Account/Timer", gotData); 
  } 

  // Whenever a query stops, start a new one.    
  $(document).ajaxStop(getData, 600000);

  // Start the first query.
  getData(); 

Here's our server code:

    public JsonResult Timer()
    {
            MembershipUser user = Membership.GetUser(User.Name);
            user.LastActivityDate = DateTime.Now;

            Membership.UpdateUser(user);

            // You can return anything to reset the timer.
            return Json(new { Timer = "reset" }, JsonRequestBehavior.AllowGet);
    }
like image 50
Fred Chateau Avatar answered Nov 06 '22 10:11

Fred Chateau


Sounds to me like you need some jQuery polling to happen.

You can easily do a jQuery post to an ActionResult which would then check for users online and returns a PartialView back to the calling jQuery function.

The returning PartialView might have all the users logged in which can then be popped up in some sort of animating panel.

Use javascript to execute a timed poll back to the controller.

like image 3
griegs Avatar answered Nov 06 '22 12:11

griegs


You cannot get onlines live in web. you should refresh page or refresh content with ajax -or else-. So it gonna solve i think.

ps. chat and online issues, you have two options; you can store them in database -its what i suggest- or store in memory, you may want to look this

Tables:

Users
-Id / int - identity
-LoginTime / datetime
-IsOnline / bit

Friends
-Id / int - identity
-FirstUserId / int
-SecondUserId / int

public class UserInformation
{
    public IList<User> OnlineFriends { get; set;}
    public IList<User> JustLoggedFriends { get; set; } /* For notifications */
}

public class UserRepository
{
    public UserInformation GetInformation(int id, DateTime lastCheck)
    {
        return Session.Linq<User>()
                .Where(u => u.Id == id)
                .Select(u => new { 
                              User = u,
                              Friends = u.Friends.Where(f => f.FirstUser.Id == u.Id || f.SecondUser.Id == u.Id)
                })
                .Select(a => new UserInformation {
                                JustLoggedFriends = u.Friends.Where(f => f.IsOnline && f.OnlineTime >= lastCheck).ToList(), 
                                OnlineFriends = u.Friends.Where(f => f.IsOnline).ToList()
                })
                .ToList();
    }
}

public class UserService
{
    public UserInformation GetInformation(int id, DateTime lastCheck)
    {
        return repository.GetInformation(id, lastCheck);
    }
}

UI:

public class UserController
{
    public ActionResult Index()
    {
        var userId = (int)Session["UserId"];
        var lastCheck = (DateTime)Session["LastCheck"];
        UserInformation info = userService.GetInformation(userId, lastCheck);
        Session["LastCheck"] = DateTime.Now;

        //show up notifications and online users.
    }
    public ActionResult Login()
    {
        User user = null; // TODO: get user by username and password
        Session["UserId"] = user.Id;
        Session["LastCheck"] = DateTime.Now;
    }
}
like image 2
cem Avatar answered Nov 06 '22 10:11

cem