开发者

Update client status to server on "onbeforeunload" event

I have a flash application that needs to send a http request to the server which will disconnect the existing session immediately. I have a tried a few options but none is reliable. Option #1: On "onbeforeunload" event send a http request from inside the flash applications. Fallacy: This does not work because as soon as the browser is closed the flash player unloads the app and hence the communication breaks.

Option #2: On "onbeforeunload" event send a http request using XMLHTTPRequest in ajax. This wor开发者_JS百科ks fine in IE but doesnt work in Firefox. When i debugged the http req in httpfox it threw "NS_BINDING_ABORTED" error which i think means that request was cancelled due to page unload.

Note that if i use an alert box, the requests are getting sent in both the options. But i cant use alert boxes. Is there any other way to do so ? or maybe kill the alert box after a timeout ?


It should not be the client's responsibility to tell the server it is disconnecting. You can make the client ping the server regularly to tell it it is still alive, or you could open an never-ending http request (comet-style), and fire the event when the tcp connection is broken.

In PHP it would look somehthing like this: (Not tested)

<?php
    // Run script until aborted.
    ignore_user_abort(true);
    set_time_limit(0);
    while(connection_status() == CONNECTION_NORMAL) {
        sleep(1);
    }

    // The connection was lost, so do something.
    onLostConnection();
?>


Anyway what was happening was that i was sending a crossdomain xmlhttprequest which is not allowed. see here, http://developer.yahoo.com/javascript/howto-proxy.html . I just fixed the urls and it worked in firefox as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜