Page 1 of 1

Comet connections remaining open in IE.

Posted: Fri Apr 03, 2009 7:03 pm
by JellyFish
It seems as though IE is keeping all comet connections open even when I refresh or load to other pages. IE's connection limit is 2 per web domain. If I refresh the page twice, the third refresh sticks, because the comet connections from the previous page loads are still alive. I know it's my comet code doing this because when I comment my comet code out, the sticking doesn't happen.

I'm not going to show you my comet code, because it's not necessarily the problem. Rather, I'm going to ask how I could close connections when the page is unloaded? I'm using jQuery to establish a long poll to a page called home_comet.php. Maybe there's a way to tell IE to close the ajax request when the page is left.

Thanks, and all help is appreciated.

[EDIT] I fortunately decided to show you some code:

Code: Select all

 
(function homeCometCallback(data)
{
    if (data)
    {
        var json = eval("("+data+")");
    
        for (var category_id in json)
        {
            var fields = json[category_id];
            var listElement = $(".catbox.category-"+category_id+" ul");
            
            //Check light or dark class
            var coloringClass = listElement.children(":first").hasClass("light") ? "dark" : "light";
            
            //Prepend data to the list element
            listElement.prepend("<li class='"+coloringClass+"'>"
                +"<a href='/view_post.php?id="+fields['id']+"'>"
                +fields['title']
                +" <span class='specific-location'>("
                +fields['specific_location']
                +")</span>"
                +"</a>"
                +"<span class='price'>$"+fields['price']+"</span>"
                +"</li>");
            
            //Delete rows after five
            listElement.children().eq(3).remove();
        }
    }
    $.get("/home_comet.php?"+Math.random(), {location: $_GET['l']||""}, homeCometCallback);
})("");
 

Re: Comet connections remaining open in IE.

Posted: Fri Apr 03, 2009 10:29 pm
by JellyFish
I think I finally figured it out, and I'm exhausted.

I didn't know jQuery's Ajax methods returned an XmlHTTPRequest object. So all I needed to do is call abort() on the XHR object returned by $.get() in the onbeforeunload event.

Code: Select all

var cometXHR;
function homeCometCallback(data)
{
    if (data)
    {
        var json = eval("("+data+")");
    
        for (var category_id in json)
        {
            var fields = json[category_id];
            var listElement = $(".catbox.category-"+category_id+" ul");
            
            //Check light or dark class
            var coloringClass = listElement.children(":first").hasClass("light") ? "dark" : "light";
            
            //Prepend data to the list element
            listElement.prepend("<li class='"+coloringClass+"'>"
                +"<a href='/view_post.php?id="+fields['id']+"'>"
                +fields['title']
                +" <span class='specific-location'>("
                +fields['specific_location']
                +")</span>"
                +"</a>"
                +"<span class='price'>$"+fields['price']+"</span>"
                +"</li>");
            
            //Delete rows after five
            listElement.children().eq(3).remove();
        }
    }
    $.get("/home_comet.php?"+Math.random(), {location: $_GET['l']||""}, homeCometCallback);
}
$(window).load(function(){
    cometXHR = $.get("/home_comet.php?"+Math.random(), {location: $_GET['l']||""}, homeCometCallback);
}).bind("beforeunload", function(){
    cometXHR.abort();
});

Re: Comet connections remaining open in IE.

Posted: Sat Apr 04, 2009 6:39 am
by josh
Interesting. Should this be built into jquery? I would consider this a bug. At least my clients would.

Re: Comet connections remaining open in IE.

Posted: Mon Apr 06, 2009 12:19 pm
by JellyFish
josh wrote:Interesting. Should this be built into jquery? I would consider this a bug. At least my clients would.
You mean to say that jQuery should handle the onbeforeunload event by default, canceling all the requests? It would seem like this is more of an IE bug then a jQuery bug, sense IE is the one that's keeping the connections open. Although, jQuery is the one that is suppose to be smoothing out these inconsistencies across browsers.