Friday, 27 January 2012

ASP: is the client still connected?

   


I recently wrote a short post about things not to forget when creating ASP document. One of the general tips I mentioned was about checking if the client is still connected, before starting long procedures like complicated server requests (i.e. SQL queries).
In this article we are going to see how to do it with the response object (IsClientConnected).

The response object
The response object is one very important thing in ASP programming. It basically sends responses from the server to the user. It has a very large list of collections, properties and methods, and it is one of those things we can't live without. We use it for managing cookies (set and/or get), controlling the cache, the buffer, the charset, and we use it for redirects, flushes and so on.
In this post we are going to use it to check if the client is still connected or not.

IsClientConnected
The IsClientConnected property tells us if the client is connected, returning a true/false value. While the syntax is very simple:
response.IsClientConnected
we can use it together with a conditional statement in order to output the client status:
<%
If Response.IsClientConnected=true then
  Response.Write("User connected")
else
  Response.Write("User not connected")
end if
%> 
If we use the above code in any of our ASP document, the only possible output is "User connected". That is because, if we (the users) make the request  to the server, it is clear that we are connected!
However, if telling the user if he/she is still connected, has no value at all, it is important to check it before starting a long and resources consuming server operation.
So, instead of outputting the result of the IsClientConnected request, we check it, and eventually go on with our long server request:
<%
If Response.IsClientConnected=true then
  *** our long server request ***
end if
%>
That's all.

Please let me know what you think about it, using the comments section below.

0 thoughts:

Post a Comment

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.