Using ASP.net to create an AJAX-based chat room program

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.