开发者

MVC Partial View with AutoRefresh

开发者 https://www.devze.com 2023-01-26 23:29 出处:网络
I need to keep a list of logged users STATUSES at the Server level (either \"ONLINE\" or \"OFFLINE\").

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 crea开发者_StackOverflowte 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>
        <%
        }
    }
%>


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>


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.


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>
<%
        }
    }
%>
0

精彩评论

暂无评论...
验证码 换一张
取 消