I work on a Comet application written in ASP.NET. There is an array of active connection contexts (HttpContext). And there is a thread that should periodically iterate through the collection and check theirs state. So application architecture is not thread-per-request.
What is the best way to check that a connection is active (not closed by the remote host)?
I found this:
context.Response.Write(' ');
context.Response.Flush();
if (!context.Response.IsClientConnected)
{
// ...
}
But it’s not a good solution because it takes a thread time to process (Flush() is blocking operation). I need solution that works very fast for many concurrent connections and don’t use blocking operations.
Maybe there is some IIS or ASP.NET functionality that allows to monitor connections this way?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The short answer is… no. The only way ASP.NET knows to return false for IsClientConnected is if the user makes a different call to the server, or Response.Close has been previously called. It’s quite possible for the user to close the connection and still have IsClientConnected return true, in my experience. Just think about the underlying HTTP communication that’s going on and you can see why.
What are you trying to accomplish?
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0