上网用户在asp.net mvc的列表 [英] get a list of online users in asp.net mvc
问题描述
我在我的应用程序,它始终显示在线用户的更新列表的页面。
现在,以保持列表的存储在应用对象 - 更新,我做以下步骤
I have a page in my application which always shows updated list of online users. Now, to keep the list-which is stored in application object- updated, i do the below steps
-
添加用户列表时登录
add user to list when login
去除注销用户
然后处理浏览器关闭/导航离开的情况下,我有一个时间戳和集合中的用户名一起
每90秒一个AJAX调用更新的时间戳。
Then to handle browser close/navigate away situations, I have a timestamp along with the username in the collection An ajax call every 90 seconds updates the timestamp.
问题:
我需要的东西清理这个列表每120秒与旧时间戳删除条目。
The problem: I need something to clean this list every 120 seconds to remove entries with old timestamps.
我如何做到这一点我的web应用程序中?即调用函数,每2分钟。
How do I do this within my web application? ie Call a function every 2 mins.
PS:我想调用web服务,每2分钟使用调度,但托管环境不允许任何调度
PS: I thought of calling a webservice every 2 mins using a scheduler , but the hosting environment do not allow any scheduling.
推荐答案
在您的账户控制器
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (HttpRuntime.Cache["LoggedInUsers"] != null) //if the list exists, add this user to it
{
//get the list of logged in users from the cache
List<string> loggedInUsers = (List<string>)HttpRuntime.Cache["LoggedInUsers"];
//add this user to the list
loggedInUsers.Add(model.UserName);
//add the list back into the cache
HttpRuntime.Cache["LoggedInUsers"] = loggedInUsers;
}
else //the list does not exist so create it
{
//create a new list
List<string> loggedInUsers = new List<string>();
//add this user to the list
loggedInUsers.Add(model.UserName);
//add the list into the cache
HttpRuntime.Cache["LoggedInUsers"] = loggedInUsers;
}
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
public ActionResult LogOff()
{
string username = User.Identity.Name; //get the users username who is logged in
if (HttpRuntime.Cache["LoggedInUsers"] != null)//check if the list has been created
{
//the list is not null so we retrieve it from the cache
List<string> loggedInUsers = (List<string>)HttpRuntime.Cache["LoggedInUsers"];
if (loggedInUsers.Contains(username))//if the user is in the list
{
//then remove them
loggedInUsers.Remove(username);
}
// else do nothing
}
//else do nothing
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
在您的局部视图。
@if (HttpRuntime.Cache["LoggedInUsers"] != null)
{
List<string> LoggedOnUsers = (List<string>)HttpRuntime.Cache["LoggedInUsers"];
if (LoggedOnUsers.Count > 0)
{
<div class="ChatBox">
<ul>
@foreach (string user in LoggedOnUsers)
{
<li>
<div class="r_row">
<div class="r_name">@Html.Encode(user)</div>
</div>
</li>
}
</ul>
</div>
}
}
使这个局部视图,当用户登录
render this partial view when user log in.
使用这个脚本调用以往90秒
use this script call ever 90 second
<script type="text/javascript">
$(function () {
setInterval(loginDisplay, 90000);
});
function loginDisplay() {
$.post("/Account/getLoginUser", null, function (data) {
});
}
</script>
这篇关于上网用户在asp.net mvc的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!