Chat Application
-
I will lead you through construction of a mini ASP.NET web chat application using the ASP.NET AJAX server centric mode.
Hitting the High Spots
As a chatting room module, its main functionalities involve: user login, ensuring authentication, sending chat messages, displaying chat messages dynamically, displaying the online user list dynamically, and so forth.
However, one of the most prominent requirements in developing a chat application lies in responding to the user in real time and displaying user information quickly so that users do not wait for a long time. Obviously, we have to display the online users’ statuses in due course: neither permitting the display of offline users as if they were online, nor enabling the display of the already-online users (except for some special cases).
To achieve this goal, you must support in-time communications between the server side and the client side. This task used to be accomplished with the help of thesetIntervalorsetTimeoutfunction of JavaScript. However, with the emergence of various AJAX frameworks (especially ASP.NET AJAX) based upon ASP.NET platform, the above two functions were rarely used.
In the ASP.NET AJAX framework, the Timer control helps, when the requirements of in-time communications and not updating the whole page can be met with the combination of the UpdatePanel control and the Timer control.
In detail, the entity objects involved in constructing a web chat room module mainly include the chat users, the chat information, and the chat room. In object-oriented terms, we must abstract and encapsulate the three objects, which are utilized to achieve the related operations in the chat room module in this article.
One of the points worth noticing is that we’ve adopted a simple way to deal with the chat user information-it is persisted in the server side buffers, not in the databases and various files.
When the user logs into the system, he has to input his account name and password. If the current chat room does not contain this account information, he can log in using any temporary password. However, if the current chat room does contain this account information, the just-entered password and the one saved in the buffer will be compared. If they are the same, the current user can log in; otherwise, he will be prompted to re-enter the information or change to other account information. Figure 1 gives the main page of the web chat sample application.As you’ve seen, herein the valid users can send and receive messages, view the current online user list, and so forth.
A Few Words About the Timer Control
There are three ASP.NET AJAX server controls introduced to manipulate and maintain the chat application, i.e. ScriptManager, UpdatePanel and Timer. Here we’re mainly interested in the Timer control, which bears the responsibility for sending the synchronous or asynchronous requests to the server at specified fixed time intervals. This control is usually used in combination with the UpdatePanel control to update some part of a page asynchronously. What’s more, it can also be used to “PostBack” the whole page in regular time.
One of the important points worth noticing is that, as an Ajax control, the Timer control is different from that defined within the ”System.Threading” namespace. The former is defined based upon the latter, with the main purpose of generating the client-side scripts so that the client side can send out requests to the server side on schedule.
For clearer reference, we’ve listed the four members of the Ajax Timer control in the table below.
Property or Event name
Description
Enabled Indicates whether to enable the tick event. Interval Specifies the interval time. Tick Specifies the task to be executed after the tick event is triggered. Implementing the Web Chatting Module
In this section, we will delve into the implementation of the web chat module, which mainly involves the general flow, the entity class, the login sub-module, and the main chat page, which will be examined in detail below one by one.
(1) The General Chat Flow
The chat flow is rather clear to follow. First, the user should log in and his identify should also be authenticated. Then, the user enters the chat main interface, where he can deliver his chat messages and view the current chat records and the online users. At the same time, the system will have to update the online user-related time information in good time, so that the valid online users will not be kicked out of the chat lobby. In a rough style, Figure 2 gives the related chat flow.
Next, let’s examine the three entity classes mention above.
(2) The Three Entity Classes
At the beginning of this article, we pointed out that the web chat room module in this sample is mainly comprised of three entity classes, i.e. the chat users, the chat information, and the chat room, where the chat room class will invoke the other two classes.
For simplicity, we’ve only listed the members of the above three classes in the figure form (see Figure 3, 4 and 5 below).
The Login Module
To identify each online user, he or she must log in first, and then enter the chat room to chat. In this case, we have merged the login and register functionalities into one module.
The Login.aspx page bears a strong similarity to most general login pages, except for the Ajax effect. Herein, three Ajax controls are utilized, i.e. ScriptManager, UpdatePanel, and UpdateProgress. Figure 6 illustrates the design-time snapshot of the Login.aspx page.
Note here the “Login” button is put inside the UpdatePanel control to achieve the Ajax effect. Moreover, we’ve also defined an UpdateProgress control to gain more friendly interaction during the course of logging in.
Next, the ASP.NET built-in validate controls are used, all of which are located outside the UpdatePanel control. When you start this page and click the “login” button you will see a humanistic and illustrative animation appear at the upper right corner of the page until the populated data have been validated, as is shown in
HTML Code Chat Application you must be registered Coder Forum and signed in coder forum to download Free Chat Application Code
Thank you for Visiting ^^!












January 13th, 2009 at 3:53 am
befoe this i have thinking about chat room inside my site but i have forgetit worry a spam will coming soon..
January 19th, 2009 at 7:46 pm
wow amazing code
April 4th, 2009 at 3:44 am
It’s very nice
June 24th, 2009 at 11:16 pm
Good site for every user.