Comet connections remaining open in IE.

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Comet connections remaining open in IE.

Post 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);
})("");
 
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Comet connections remaining open in IE.

Post 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();
});
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Comet connections remaining open in IE.

Post by josh »

Interesting. Should this be built into jquery? I would consider this a bug. At least my clients would.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Comet connections remaining open in IE.

Post 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.
Post Reply