PDA

View Full Version : Pinger - Sends requests at regular intervals



para
28 Jan 2008, 10:36 AM
This is a duplicate of a post in the 1.x forums, but it works on my 2.0 app, so I figured I'd add it here too...

Here's a basic class I made to ping an address at regular intervals.
I use this to keep the user's session alive while they are doing static work on the page. Essentially, while they are on a certain section of my page, there is the potential for their session to timeout, which should not occur.

It extends Ext.data.Connection, so all the parameters of that are available. I provide a couple more: autoStart and timer.

It doesn't have too many useful applications, but it does what I need it to.


Example use:


configs:
autoStart: 'true' to automatically start pinging immediately.
timer: time between pings (approximate, depending on request time)
params: params to include with the request.

// This will request 'myPage.html' every 5 minutes.
var pinger = new Pinger({url:'myPage.html', timer:300000, autoStart:true});

//or

var pinger = new Pinger({url:'myPage.html', timer:300000});
pinger.start();



EDIT:
Some events are also provided:


pinger.on('beforeping', function() {});

pinger.on('afterping', function(options, success, response) {});

// can override callback (original callback is empty)
pinger.callback = function(options, success, response) {}



Updated to remove all defered ping calls when stopping. Possible for users to cause irregular pings if turning pinging on/off.

dantheman
28 Jan 2008, 11:20 AM
Hey Para,

I've done something similar for our kerberized env.
Because XHR's won't honor re-directs (in case of
failed kerb auth), I had to use a hidden iframe for
a full page load, but the idea is the same.

Thanks for the extension.
--dan

thejoker101
28 Jan 2008, 11:40 AM
Hey Para,

I've done something similar for our kerberized env.
Because XHR's won't honor re-directs (in case of
failed kerb auth), I had to use a hidden iframe for
a full page load, but the idea is the same.

Thanks for the extension.
--dan

Technically it will I believe. I've had XHR requests get redirected and of course Ext will handle it as a failure to connect and you can retrieve the status code via (for example a form): action.response.status (where action is the second argument in the fail callback)

dantheman
28 Jan 2008, 1:37 PM
Technically it will I believe. I've had XHR requests get redirected and of course Ext will handle it as a failure to connect and you can retrieve the status code via (for example a form): action.response.status (where action is the second argument in the fail callback)By honor, I meant do-the-right-thing .. :)

Yes, I could wrap the XHR up to mimic a full page load behavior
(and others have).. I just think a hidden iframe with a keepalive
is conceptually simpler and better (as in likely more robust, imo).

YMMV, :D
--dan

para
28 Jan 2008, 1:57 PM
This script works in several applications for me now. The original use was maintaining the session for when the user was on my design application page. Incredibly, the user has the potential to spend a full work day on the page without actually hitting any other pages.

Since then I've used it for some automated pages which simply refresh, kind of like polling for info like system monitors and news collaboration page.

I know it's a pretty simple structure, but it's come in handy for my needs.