0

I have an application which uses an open JQuery Ajax connection to do long-polling/comet handling of updates.

Sometimes the browser and the server lose this connection (server crashes, network problems, etc, etc).

I would like the client to detect that the update has crashed and inform the user to refresh the page.

It originally seemed that I had 2 options:

  • handle the 'error' condition in the JQuery ajax call
  • handle the 'complete' condition in the JQuery ajax call

On testing, however, it seems that neither of these conditions are triggered when the server aborts the query.

How can I get my client to understand that the server has gone away?

Gordon Guthrie
  • 6,252
  • 2
  • 27
  • 52

1 Answers1

1

Isn't it possible to add a setInterval() function that runs every few seconds or minutes? That way you can trigger a script that checks whether the server is still up, and if not, reset the comet connection. (I don't know what you use for the long-polling exactly though, so I don't know if it's possible to reset that connection without a page reload. If not, you can still display a message to the user).

So something like this:

var check_server = setInterval(function() {
  // run server-check-script...
  // if (offline) { // reset }
}, 60000);
Alec
  • 9,000
  • 9
  • 39
  • 43
  • That would work - it just seems a bit ugly when I have a connection that is aborting and I just want it to tell me it has gone away. – Gordon Guthrie Jan 17 '11 at 10:23
  • Well, when the connection dies, so does its ability to tell you that it has done so :) – Alec Jan 17 '11 at 10:36
  • Yeah, but the browser is told that the long-poll open connection is away and Firebug marks the query as red in the javascript console, so somebody tells somebody it has gone. I just feel left out :( – Gordon Guthrie Jan 17 '11 at 11:58
  • Might this be related? http://stackoverflow.com/questions/1002367/jquery-ajax-jsonp-ignores-a-timeout-and-doesnt-fire-the-error-event. I don't know if you're using JSONP. And does changing/setting the timeout variable in the AJAX call change anything? – Alec Jan 17 '11 at 16:09