que usar jQuery para realizar una simple llamada AJAX a un controlador HTTP ficticia que no hace más que mantener mi sesión activa:
function setHeartbeat() {
setTimeout("heartbeat()", 300000); // every 5 min
}
function heartbeat() {
$.get(
"/SessionHeartbeat.ashx",
null,
function(data) {
//$("#heartbeat").show().fadeOut(1000); // just a little "red flash" in the corner :)
setHeartbeat();
},
"json"
);
}
gestor de sesiones puede ser tan simple como:
public class SessionHeartbeatHttpHandler : IHttpHandler, IRequiresSessionState
{
public bool IsReusable { get { return false; } }
public void ProcessRequest(HttpContext context)
{
context.Session["Heartbeat"] = DateTime.Now;
}
}
El la clave es agregar IRequiresSessionState; de lo contrario, Session no estará disponible (= null). El controlador también puede devolver un objeto serializado JSON si algunos datos se deben devolver al JavaScript que realiza la llamada.
disponibles a través de web.config:
<httpHandlers>
<add verb="GET,HEAD" path="SessionHeartbeat.ashx" validate="false" type="SessionHeartbeatHttpHandler"/>
</httpHandlers>
añadió de balexandre el 14 de agosto, 2012
me gustó tanto de este ejemplo, que quiero mejorar con el HTML/CSS y la parte de compás
cambiar esto
//$("#heartbeat").show().fadeOut(1000); // just a little "red flash" in the corner :)
en
beatHeart(2); // just a little "red flash" in the corner :)
y añadir
// beat the heart
// 'times' (int): nr of times to beat
function beatHeart(times) {
var interval = setInterval(function() {
$(".heartbeat").fadeIn(500, function() {
$(".heartbeat").fadeOut(500);
});
}, 1000); // beat every second
// after n times, let's clear the interval (adding 100ms of safe gap)
setTimeout(function() { clearInterval(interval); }, (1000 * times) + 100);
}
HTML y CSS
<div class="heartbeat">♥</div>
/* HEARBEAT */
.heartbeat {
position: absolute;
display: none;
margin: 5px;
color: red;
right: 0;
top: 0;
}
aquí es un ejemplo vivo por sólo la parte golpeo: http://jsbin.com/ibagob/1/
Puede comprobar este enlace para obtener contestar así http://www.dotnetcurry.com/ShowArticle.aspx?ID=453 –