asp.net MVC 2:在线用户 [英] asp.net mvc 2: online user

查看:120
本文介绍了asp.net MVC 2:在线用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在asp.net MVC2,我怎么显示用户的状态为联机?

In asp.net mvc2, how do I show the status of a user as "online"?

因此​​,如果用户正积极在网站上,状态将是在线。

So if the user is actively working on the site, the status would be "online".

如果用户走下场地约5分钟车程那么这将是五分钟。

if the user stepped away from the site for about 5 minutes then it would be "5 minutes".

如果用户走下场地约10分钟车程那么这将是10分钟。

if the user stepped away from the site for about 10 minutes then it would be "10 minutes".

等等等等等等。

什么是实现这一目标的最佳途径?任何code样品是对我非常有帮助的。

What is the best way to accomplish this? Any code sample would be very helpful to me.

答复迄今建议我用阿贾克斯。如果是,那么我将如何能够查询在线用户VS脱机用户。我所有的疑问违背数据库。是否可以查询并显示结果加入阿贾克斯的结果与数据库查询?

The responses so far suggest that I used Ajax. If so, then how would I be able to query online users vs offline users. All my queries go against Database. Is it possible to query and display results joining Ajax results with Database queries?

推荐答案

我想这样做将是一个会话变量的最直接的方式。你可以一行添加到您的控制器(无论是在操作方法或构造,甚至可能有动作过滤器),它的储藏当前日期/时间的会​​话。然后,您可以使用Ajax调用在特定的时间间隔来更新屏幕上的价值。你可能会想在分钟而不是秒,否则你会显示一个计数器(即1秒,2秒等)的时间间隔。

I would think the most straight forward way of doing it would be with a session variable. You could add a line to your controllers (either in the action method, or the constructor, or even possibly with an action filter) which stashes the current Date/Time in the session. You could then use an ajax call to update the value on the screen at a specific interval. You would probably want to make the interval in minutes rather than seconds otherwise you would be displaying a counter (i.e. "1 second", "2 seconds", etc).

一些快速code样本:

Some quick code samples:

// Somewhere in controller
Session["LastSeen"] = DateTime.Now;

// Now, an action that would return the amount of time since the user was last seen
public ViewResult GetLastSeenTime()
{
    return Json(new { TimeAway = Date.Time.Now.Subtract((DateTime)Session["LastSeen"]).TotalMinutes});
}

// Then on your page, something like this
$.post("/Controller/GetLastSeenTime",null,function(result) {
    if(result.LastSeen < 5)
        $("#Status").Text("Online");
    else if (result.LastSeen % 10 == 0)
        $("#Status").Text(result.LastSeen + " minutes");
 },"json");

完全不是测试,但应靠近。

Totally not tested, but should be close.

这篇关于asp.net MVC 2:在线用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆