Creating a Simple Chat application in .Net

Chat applications are found in almost all the sites that are interactive with the users. Chat rooms in the websites provide a means of communicating with the customer service cell of any company so that the customer gets the first hand information on any products and services that is on display in the site.


Chat applications with many features are even sold for a sum but some sites may require a very simple feature to interact with the customer service personnel who is online. Such simple chat applications can be built on your own. Let us see some algorithm and the code snippets that go in the way of creating a simple chat application.

To store the messages that are typed during a chat you can either use static arrays or use a database for that purpose. Using a database is a good way of storing messages. However you can also use static arrays for that purpose. The chat application that we are going to see will work on any browser that supports <iFrame>, since we are using <iFrame> to display the chat window of the application. We will be refreshing the page in the <iFrame> to refresh the chat window alone without refreshing the whole page in which the <iFrame> is present.

To enter the chat you need an .aspx page where the user enters the name of the room and the user id. Then the user presses a Join chat button. You can pass these parameters to the ChatPage.aspx where you will be having an iFrame for displaying the messages and a text box control to type the messages and a button to send the typed messages. To pass the parameters collected from the user to the ChatPage.aspx you can use the following code which looks like,

private void Join_Click(object sender, System.EventArgs e)
{
Response.Redirect("ChatPage.aspx?Room=" + Room.Text + "&User=" + UserID.Text);
}
For the purpose of creating the chat application you may need to create a class that is used for performing various functions in the chat like adding messages that are typed into a static array. You can create any class for this purpose. To add messages you can have methods within the class for this purpose. Methods for retrieving the entire message typed, and methods for displaying the messages in the chat window are also needed.

A code snippet for adding messages can be something like given below.

static protected ArrayList stArray = new ArrayList();
static public void AddMesg(string sRoom, string sUser, string sMsg)
{
string sAddTxt = sRoom + ":" + sUser + ":" + sMsg;
pArray.Add(sAddTxt);

if ( stArray.Count > 100 )
{
stArray.RemoveRange(0,20);
}
}

Within the ChatPage.aspx, once you type the message that is to be sent and when you click the Send button, you need to add the message that is typed. You check the text box for that purpose and if it contains any text you just add the message to the static array.

private void Send_Click(object sender, System.EventArgs e)
{
string sRoom = "";
string sUser = "";

if ( Request.Params["Room"] != null )
sRoom = Request.Params["Room"].ToString();
else
sRoom = "1";

if ( Request.Params["User"] != null )
sUser = Request.Params["User"].ToString();
else
{
sUser = "Anonymous User";
}


if ( TB_ToSend.Text.Length > 0)
{
PageModule.Chat.AddMessage(sRoom,
sUser,
ToSend.Text);

ToSend.Text = "";
}
}

You can use the following code to refresh the page in the <iFrame> to display all the messages that are typed.

Response.Write( "<meta http-equiv=\"Refresh\"content=\"4\">" );

Call the method that is created to display all the messages in the <iFrame> after refreshing the page.

You can thus create a simple chat application which can be used in all the sites that need interaction with the user.

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.