具有自动刷新功能的 MVC 部分视图

发布于 2024-10-03 11:54:21 字数 1173 浏览 4 评论 0原文

我需要在服务器级别保留已登录用户状态的列表(“在线”或“离线”)。

所以我写了一个Partial View来维护用户当前的状态(在线,离线)。 服务器将这些值存储在数据库中,并将所有当前在线用户存储在缓存条目中,以便我可以从缓存中检索所有当前“在线”用户的列表。

为了保持此列表最新,我现在需要一个异步 AutoRefresh 调用,通知服务器将我的用户 ID 保留在 ONLINE 列表中。此调用应每 xx 秒执行一次,并且仅在当前状态为 ONLINE 时执行。

问题:

  1. 如何创建每 XX 秒触发一次的 AutoRefresh 调用
  2. 如何确保此调用仅在我处于在线状态时执行

提前致谢。


这是有问题的部分视图。 您建议我将运行自动刷新的代码放在哪里(母版页、主视图、部分视图)???

<%@ 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:

  1. How can I create an AutoRefresh call that fires every XX seconds
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦行七里 2024-10-10 11:54:21

你们俩都让我走上了正确的方向,最终的“有效”答案是:

<script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function () {
        setInterval(function {
                         var url = '<%: Url.Action("StillOnline", "Account") %>';
                         $.getJSON(url, null, function() { });}
                    , 10000);
                });
</script>

Both of you put me on the right direction and the final "working" answer is:

<script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function () {
        setInterval(function {
                         var url = '<%: Url.Action("StillOnline", "Account") %>';
                         $.getJSON(url, null, function() { });}
                    , 10000);
                });
</script>
怀中猫帐中妖 2024-10-10 11:54:21

这是一些带有ajax调用和递归函数调用(异步)的js,

var onlineupdate;
(onlineupdate = function()
{
   if(online())
   {
     $.post('serverside url', data, function(){
        setTimeout(onlineupdate,XX);
     });
   }
   else
   {
       setTimeout(onlineupdate,XX);
   }
})()

再次需要确定用于确定什么算作在线的函数。

here's some js with an ajax call and recursive function calls (asynchronous)

var onlineupdate;
(onlineupdate = function()
{
   if(online())
   {
     $.post('serverside url', data, function(){
        setTimeout(onlineupdate,XX);
     });
   }
   else
   {
       setTimeout(onlineupdate,XX);
   }
})()

again the function for determining what counts as online needs to be determined.

泪眸﹌ 2024-10-10 11:54:21

使用 javascript,您可以设置一个函数来为您执行此操作

setInterval(function() {
    if (I_AM_ONLINE) {
        window.location.reload(true);
        //Or instead of refreshing the page you could make an ajax 
        //call and determing if a newer page exists.  IF one does then reload.
    }
}, 300000);

,其中 300.000 是每次调用之间的毫秒数(5 分钟)。

I_AM_ONLINE 仍然是最难的部分,并且取决于很多事情......

编辑

我会在部分本身内部添加此代码(最好在其末尾):

<%  if (MySite.Security.SiteUser.IsAuthenticated) {
        if (Convert.ToBoolean(ViewData["IsLogged"])) { %>
            <script type="text/javascript">
            setInterval( window.location.reload(true), 300000);
            </script>
<%
        }
    }
%>

Using javascript you can setup a function that does it for you

setInterval(function() {
    if (I_AM_ONLINE) {
        window.location.reload(true);
        //Or instead of refreshing the page you could make an ajax 
        //call and determing if a newer page exists.  IF one does then reload.
    }
}, 300000);

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):

<%  if (MySite.Security.SiteUser.IsAuthenticated) {
        if (Convert.ToBoolean(ViewData["IsLogged"])) { %>
            <script type="text/javascript">
            setInterval( window.location.reload(true), 300000);
            </script>
<%
        }
    }
%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文