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.
Using the Code
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");
}
protected void Page_Load(object sender, EventArgs e)
{
string sUser = "";
try
{
sUser = Session["UserLogin"].ToString();
}
catch
{
sUser = "";
}
if (!String.IsNullOrEmpty(sUser))
{
this.Controls.Clear();
string sCommand = Request["cmd"];
if (!String.IsNullOrEmpty(sCommand))
{
switch (sCommand.ToLower())
{
case "logout":
try
{
string sUsername = Session["UserLogin"].ToString().ToLower();
Session["UserLogin"] = "";
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":
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":
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":
try
{
Response.Write("<p align=center><b>Member List</b></p>");
foreach (Member mem in CashMember.arrMember)
{
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