Partial MVC View with AutoRefresh

I need to save the list of registered STATUS users at the server level (either "ONLINE" or "OFFLINE").

So, I wrote a Partial View to maintain the current status of the user (online, offline). The server stores these values ​​both in the database and in all current Internet users, as well as in the Cached entry, so that I can get a list of all current "Online" users from the cache.

To save this uptodate list, I now need an asynchronous AutoRefresh call that notifies the server that my user ID is saved in the ONLINE list. This call must be made every xx seconds and should only be made if the current state is ONLINE.

QUESTIONS:

  • How to create an AutoRefresh call that runs every XX seconds
  • How can I guarantee that this call is made only when I am in ONLINE status.

Thanks in advance.


This is a partial view in question. Where do you suggest I put the code to run 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> <% } } %> 
+4
source share
3 answers

Both of you put me in the right direction, and the final "working" answer:

 <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> 
+3
source

Using javascript, you can customize 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 remains the hardest part and depends on many things ....

EDIT

I would add this code inside the particle 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> <% } } %> 
+1
source

here are some js with ajax calls and recursive functions (asynchronous)

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

again, you need to define a function to determine what is considered online.

+1
source

All Articles