My first Chat Room was written in ASP 3.0. The program is relatively simple. Two Text Box is used to process the information refresh on the page. At that time, to build a real Chat Room, Java Applet or Activex Control must be used. HTTP -based Chart Rooms faced the same questions as my first Chat Room. These problems include the flashing phenomenon of the screen caused by the page refresh. But this problem has been solved by AJAX. AJAX is a combination of JavaScript and XML asynchronous calls. Now use some JavaScript code on the server to achieve a real Chat Room. This article will not introduce Ajax, and assumes that you have a certain understanding of the use of Ajax and ASP.NET. Just introduce how to use AJAX technology to create a basic Chat Room.
Routine
This is a single Chat Room of a multi -user. It can achieve basic chat functions, and also supports some command lines such as: /admin clear to clear the chat history, /nick [name] to change the user's nickname. The program illustrates this program to process all chat information and user information with a Chatedine class. User information is stored in a hashtable and chat information is stored in StringCollection.
Hashtable users;
StringCollection Chat;
Declars a global ChatEngine instance in global.asax.cs, sharing all Users in Chat Room:
Public Static UChat.Chatngine.ichatngine Engine = New UChat.Chatngine.ChatEngine ();
There is also a JavaScript Timer function used 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 Alreamy
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 (String.Format (
JoineDFMT, User))));
}
}
Program running interface
Start page shows some basic information about the current session, such as User Number, the size of the chat history. Users must provide a username to enter the chat room. Click Login Button to enter the function below:
Protected Void Login (Object Sender, EVENTARGS E)
{{
string user = txtusername.text;
if (! Validatenick (user)) Return;
if (global.engine.userexist (user))
{{
lblerrormsg.text = a user with this +
name Alream EXISTS, TRY AGAIN.;
Return;
}
Response.redirect (server.aspx? Action = login & u = + user);
}
After some simple verification, the user is added to User Lists via the ADDUSER function, and then entering the chat room page Chat.aspx. At this time, the javascript function below will be executed:
<script type = Text/JavaScript>
sniffbrowsertype ();
// shows loading .. screen
showloadscreen ();
// set the javascript timeer and
// Loads User List and Messages
settimers ();
setfocus ('mytext');
</script>
When the user types some information and returns, the following functions will be called:
<input type = text class = mytext
id = MyText Onedown = CAPTURERETURN (Event)>
// Capture the Enter key on the input box and post message
Function CaptuRereturn (EVENT)
{{
if (event.which || event.keycode)
{{
If ((Event.which == 13) || (Event.keycode == 13))
{{
posttext ();
Return false;
}
else {
Return true;
}
}
}
Function Posttext ()
{{
RND ++;
// Clear Text Box first
Chatbox = getelement (mytext);
Chat = Chatbox.value;
Chatbox.value =;
// get user guid from url
userid = logation.search.substring (1, locally.search.length);
// Construct Ajax Server URL
url = 'Server.aspx? Action = Postmsg & U =' + Userid + '& T =' +
enCodeuricomPonent (Chat) + '& session =' + RND;
// Create and set the instance
// of appropriant XMLHTTP Request Object
REQ = Getajax ();
// Update Page with New Message
Req.onReadyStateChange = Function () {
if (Req.readyState == 4 && Req.status == 200) {{
updateall ();
}
}
Req.open ('Get', URL, TRUE);
REQ.SEND (NULL);
}
There are so many, there is nothing special, you can see the source code, there are a lot of annotation information in it.
in conclusion
To build a Chat Room with Java Applet, you need to install JVM on the user's machine. There are some security problems with Activex Control. With the AJAX just introduced, you can easily create a chat room program based on HTTP that does not require users to install any software, and it is easy to maintain.