Static News Archive



   (9-Sep-09 18:38)  Connection limit in IE and XMLHTTP Abort
With IE's well known connection limit of 2, we need to be careful about what we do with those connections.
I often use one of the connections for singleshot, fast responding ajax queries, and the other for long waiting queries.

If the response from the long running query is no longer needed, then we need to shut down the http request, so we can reuse the connection to the server.

For IE, there is a nice trick to doing this.
On your client side code, call the normal oXmlHTTPObj.abort() function.
Now, your server code needs to know that it has stopped being requested. To do this (and break from your long running operation), check the IsClientConnected property in the Response:

while (true) {
// ... Do stuff
Thread.Sleep(100);
if (Context.Response.IsClientConnected) {
break;
}
}


This IsClientConnected property only seems to get set when the request comes from IE - but that's OK, since other browsers don't seem to suffer from this connection limit.
(Of course, it would be nice for the server to always know when the connection has gone away, regardless of the requesting browser - any suggestions?)


One thing to be aware of though!
You may find that calling oXmlHTTPObj.abort() doesn't actually close the connection (therefore, the IsClientConnected flag doesn't get cleared)!
I have found that a lot of xml http examples include the following snippet before sending off the request to the server:
m_xmlHttpObj.setRequestHeader("Connection", "Close");

Simply remove this addition to your request header, and the IsClientConnected property will function normally.

(This seems to be a fix for firefox 1.5 (see here), but since firefox 1.5 is long gone, we probably don't need it any longer...)

-Rob-

Post a comment     

<-  (18-Jun-09 23:57)  Horrible CLR context... (13-Dec-09 21:58)  Simple error slider ...  ->













(sitemap)