Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Ajax Basic - Ajax Member Chat (AMC)

3.18/5 (5 votes)
28 Jul 2007CPOL 1   575  
Create a website for online chat using Ajax Basic and ASP.NET 2.0 with C#
Screenshot - 11.jpg

Introduction

This article describes how to create a website for online chat by using Ajax Basic and ASP.NET 2.0 with C#.

Background

See demo here.

Screenshot - 22.jpg

Screenshot - 33.jpg

Using the Code

C#
//
// Login
//
protected void btn_login_Click(object sender, EventArgs e)
{
CashMember.iTotalMember++;
Array.Resize(ref CashMember.arrMember, CashMember.iTotalMember);
int iIndex = CashMember.iTotalMember - 1;
CashMember.arrMember[iIndex].sUserName = tbx_user.Text.Trim();
CashMember.arrMember[iIndex].sPassWord = tbx_pass.Text.Trim();
CashMember.arrMember[iIndex].sIP = Request.UserHostAddress;
CashMember.arrMember[iIndex].iTotalMes = 0;
CashMember.arrMember[iIndex].sStatus = Status.IsOffLine;
CashMember.arrMember[iIndex].sLastDate = DateTime.Now.ToString();
Session["UserLogin"] = tbx_user.Text.Trim();
Response.Redirect("Chat.aspx");
}
// Server.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
//============================================================
// Check Login
//============================================================
string sUser = "";
try
{
sUser = Session["UserLogin"].ToString();
}
catch
{
sUser = "";
}
if (!String.IsNullOrEmpty(sUser))
{
//============================================================
// Clear Control
//============================================================
this.Controls.Clear();
//============================================================
// Switch Command Name
//============================================================
string sCommand = Request["cmd"];
if (!String.IsNullOrEmpty(sCommand))
{
switch (sCommand.ToLower())
{
case "logout":
try
{
string sUsername = Session["UserLogin"].ToString().ToLower();
Session["UserLogin"] = "";
// Remove User out MemberList
for (int i = 0; i < CashMember.arrMember.Length; i++)
{
if (sUsername.Equals(CashMember.arrMember[i].sUserName.ToLower()))
{ 
int iUp = CashMember.arrMember.GetUpperBound(0);
CashMember.arrMember[i] = CashMember.arrMember[iUp];
CashMember.iTotalMember--;
Array.Resize(ref CashMember.arrMember, CashMember.iTotalMember);
}
}
}
catch { }
Response.Redirect(Request.Url.ToString());
break;
case "clear":
try
{
Session["UserLogin"] = "";
CashMember.iTotalMember = 0;
CashMember.arrMember = new Member[1];
}
catch { }
Response.Redirect(Request.Url.ToString());
break;
case "getdata":
//============================================================
// Return Data to Client
//============================================================
string sFromUserGetData = Request["fromusergetdata"];
try
{
for (int i = 0; i < CashMember.arrMember.Length; i++)
{
if (CashMember.arrMember[i].sUserName.ToLower().Equals(sFromUserGetData.ToLower()))
{
if (CashMember.arrMember[i].sMessenger.Length > 0)
{
string sCtrID = CashMember.arrMember[i].sMessenger[0].sControlID;
string sContent = CashMember.arrMember[i].sMessenger[0].sUserName + ": " + 
	CashMember.arrMember[i].sMessenger[0].sContent + "<br>";
Response.Write(sContent);
int upBound = CashMember.arrMember[i].sMessenger.GetUpperBound(0);
CashMember.arrMember[i].sMessenger[0] = CashMember.arrMember[i].sMessenger[upBound];
CashMember.arrMember[i].iTotalMes--;
Array.Resize(ref CashMember.arrMember[i].sMessenger, CashMember.arrMember[i].iTotalMes);
}
}
}
}
catch { }
break;
case "senddata":
//============================================================
// Get Data from Client and Save
//============================================================
string url = Request.Url.ToString();
string sToUser = Request["touser"];
string sFromUser = Request["fromuser"];
string sCtrID2 = Request["controldisplay"];
string sContent2 = Request["msg"];
try
{
for (int i = 0; i < CashMember.arrMember.Length; i++)
{
if (CashMember.arrMember[i].sUserName.ToLower().Equals(sToUser.ToLower()))
{
CashMember.arrMember[i].iTotalMes++;
Array.Resize(ref CashMember.arrMember[i].sMessenger, CashMember.arrMember[i].iTotalMes);
int ind = CashMember.arrMember[i].iTotalMes - 1;
CashMember.arrMember[i].sMessenger[ind].sUserName = sFromUser;
CashMember.arrMember[i].sMessenger[ind].sControlID = sCtrID2;
CashMember.arrMember[i].sMessenger[ind].sContent = sContent2;
}
}
}
catch { }
break;
case "getmemberlist":
//============================================================
// Return Member List to Client
//============================================================ 
try
{
Response.Write("<p align=center><b>Member List</b></p>");
foreach (Member mem in CashMember.arrMember)
{
//NewDivMessenger(sNickName)
Response.Write("<img src=\"Images/user.png\" /> 
	<a href=\"javascript:NewDivMessenger('"+ mem.sUserName +"')\" ><b>" + 
	mem.sUserName + "</b></a><br>");
} 
}
catch { }
break;
}
}
}
}

You can take a look at the source code.

Contact

Email: thuthuattinhoc@yahoo.com
Phone: 0957736345

History

  • 28th July, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)