To download the source code, please go to the original address: http://www.codeproject.com/Ajax/UChat.asp
Brief introduction
My first chat room was written in ASP 3.0. The program is simpler, with two text box, which is used to process information refreshed per second on the page. Then, to build a real chat room, use Java applets or ActiveX control. Chart rooms, based on HTTP, faces some of the same problems as my first chat room. These issues include screen flicker caused by page refreshes. But the problem has been solved by Ajax. Ajax is a combination of JavaScript and XML asynchronous invocations. Now, with some JavaScript code on the server side, you can implement a real chat room. This article does not introduce Ajax, and assumes you have a certain understanding of Ajax and ASP.net applications. Just describes how to use AJAX technology to create a basic chat room.
Routine
This is a single chat room for multiple users. Basic chat functionality is possible, and some command lines such as/admin clear are used to purge chat records,/nick [Name] To change user nicknames, and so on. Program Description This program uses a Chatengine class to handle all the chat information and user information, the user information is stored in a Hashtable, and the chat information is stored in the StringCollection.
Hashtable users;
StringCollection chat;
Declare an instance of a global chatengine in Global.asax.cs and share it for all users in Chat room:
public static UChat.ChatEngine.IChatEngine Engine = new UChat.ChatEngine.ChatEngine ();
There is also a JavaScript timer function to synchronize global variables and page information.
function setTimers()
{
timeID = window.setTimeout( "updateAll()", refreshRate );
}
Each user is uniquely identified by a username and a GUID.
public void AddUser(string id, string user)
{
//make sure user name does not exist already
if( !UserExists( user ) )
{
//add user to users list
users.Add( id, user );
//display a notification message to all users
chat.Add( this.MakeServerMessage(string.Format(
joinedfmt, user ) ));
}
}
Program Run interface