javascript - Ajax - Checking for connection drops -



javascript - Ajax - Checking for connection drops -

i writing web chat application. uses long polling mechanism. send request server, , server holds until there info send back. responds, , client sends request server. , cycle repeats.

however, there problem - have no error checking in place. if connection drops? let's i'm chatting friend , wifi drops. internet's downwards , let's goes min later. long-polling mechanism has died , no polling, have refresh page.

how can implement error checking mechanism solve problem of unsuccessful or dropped network connections? using jquery facilitate ajax requests if helps.

*edit: here js code polling mechanism: *

// polls server more data, regular encrypted request minte.network.poll = function() { // if client not connected, not send poll requests if (!minte.client.connected) return; minte.network.request({ "action" : "poll"}, function(data) { // if client has disconnected , poll returns afterwards, not process if (!minte.client.connected) return; minte.network.lastpoll = minte.clock.gettime(); minte.processor.process(data); minte.network.poll(); }); }; // send regular, routine encrypted request minte.network.request = function(data, callback) { $.post(server_uri, data, function(data, textstatus, jqxhr) { info = json.parse(data); callback(data); }); };

first off, can hook error handling of ajax call. if fails, should error status back. since haven't seen defined anywhere, you'd have work out way test how different browsers respond when connection lost in different ways see if utilize exclusively. in case, won't wound hook ajax failure response. want implement own timeout if haven't heard server in xx time, there must sort of problem.

second off, if want quicker feedback , more consistent in different browsers, can have server homecoming heartbeat. so, ever xx seconds of inactivity, server homecoming heartbeat response. client wait, xx + yy seconds (yy allow roundtrip time , slight lag in server response) , if didn't receive either actual datagram server or heartbeat response, assume connection has died. if receives heartbeat, makes request again. can set heartbeat whatever interval want, though short times more taxing on server infrastructure.

fyi, jquery has global ajax error handler described here: http://api.jquery.com/ajaxerror#callback , jquery ajax phone call allow specify timeout , error handler.

in pseudo code:

$.ajax({ url: 'xxxx', success: function(data, textstatus, jqxhr) { if (data heartbeat) start new ajax connection inquire 1 time again if (data has returned info in it) process }; error: function(jqxhr, textstatus, errorthrown) { sort of connection error has happened }; timeout: 2 * 60 * 1000; // 2 minutes });

javascript jquery ajax networking

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -