具有自动刷新功能的 MVC 部分视图
我需要在服务器级别保留已登录用户状态的列表(“在线”或“离线”)。
所以我写了一个Partial View来维护用户当前的状态(在线,离线)。 服务器将这些值存储在数据库中,并将所有当前在线用户存储在缓存条目中,以便我可以从缓存中检索所有当前“在线”用户的列表。
为了保持此列表最新,我现在需要一个异步 AutoRefresh 调用,通知服务器将我的用户 ID 保留在 ONLINE 列表中。此调用应每 xx 秒执行一次,并且仅在当前状态为 ONLINE 时执行。
问题:
- 如何创建每 XX 秒触发一次的 AutoRefresh 调用
- 如何确保此调用仅在我处于在线状态时执行
提前致谢。
这是有问题的部分视图。 您建议我将运行自动刷新的代码放在哪里(母版页、主视图、部分视图)???
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%
if (MySite.Security.SiteUser.IsAuthenticated)
{
if (Convert.ToBoolean(ViewData["IsLogged"]))
{
%>
<div id="onlineStatus">
You are currently ONLINE >>
<%: Html.ActionLink("Take a Break", "GoOffline", "Account")%>
</div>
<%
}
else
{
%>
<div id="offlineStatus">
Ready for business >>
<%: Html.ActionLink("Go Online", "GoOnline", "Account")%>
</div>
<%
}
}
%>
I need to keep a list of logged users STATUSES at the Server level (either "ONLINE" or "OFFLINE").
So I wrote a Partial View to maintain the user current Status (Online, Offline).
The server stores these values both on the Database and all the current Online users also in a Cached entry so that I can retrieve the list of all current "Online" users from cache.
In order to keep this list uptodate, I now need an asynchronous AutoRefresh call that notifies the server to keep my userID on the ONLINE list. This call should execute every xx seconds and should only execute if the current status is ONLINE.
QUESTIONS:
- How can I create an AutoRefresh call that fires every XX seconds
- How can I ensure this call executes only when I'm in ONLINE status
Thanks in advance.
This is the Partial View in question.
Where do you suggest I put the code to run the AutoRefresh (MasterPage, Main View, Partial View) ???
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%
if (MySite.Security.SiteUser.IsAuthenticated)
{
if (Convert.ToBoolean(ViewData["IsLogged"]))
{
%>
<div id="onlineStatus">
You are currently ONLINE >>
<%: Html.ActionLink("Take a Break", "GoOffline", "Account")%>
</div>
<%
}
else
{
%>
<div id="offlineStatus">
Ready for business >>
<%: Html.ActionLink("Go Online", "GoOnline", "Account")%>
</div>
<%
}
}
%>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你们俩都让我走上了正确的方向,最终的“有效”答案是:
Both of you put me on the right direction and the final "working" answer is:
这是一些带有ajax调用和递归函数调用(异步)的js,
再次需要确定用于确定什么算作在线的函数。
here's some js with an ajax call and recursive function calls (asynchronous)
again the function for determining what counts as online needs to be determined.
使用 javascript,您可以设置一个函数来为您执行此操作
,其中 300.000 是每次调用之间的毫秒数(5 分钟)。
I_AM_ONLINE 仍然是最难的部分,并且取决于很多事情......
编辑:
我会在部分本身内部添加此代码(最好在其末尾):
Using javascript you can setup a function that does it for you
where 300.000 is the number of milliseconds between each call (5 minutes).
I_AM_ONLINE remain the hardest part and depends on many things....
EDIT:
I would add this code inside the partial itself, (preferably at the end of it):