Click here to Skip to main content
16,021,765 members
Articles / Programming Languages / C#

Ajax Basic - Ajax Member Chat (AMC)

Rate me:
Please Sign up or sign in to vote.
3.18/5 (5 votes)
28 Jul 2007CPOL 54K   575   29   18
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)


Written By
Vietnam Vietnam
Khong co mo ta

Comments and Discussions

 
GeneralReally Appreciate Pin
love_chopra17-Mar-08 23:33
love_chopra17-Mar-08 23:33 
GeneralRe: Really Appreciate Pin
Jahangir Shahzad13-Jul-10 21:45
Jahangir Shahzad13-Jul-10 21:45 
Questioncan we make it standard? Pin
Jahangir Shahzad31-Dec-07 17:54
Jahangir Shahzad31-Dec-07 17:54 
AnswerRe: can we make it standard? Pin
Govardhana Reddy26-Feb-08 6:19
professionalGovardhana Reddy26-Feb-08 6:19 
Generalproblems in firefox Pin
Jahangir Shahzad3-Nov-07 19:17
Jahangir Shahzad3-Nov-07 19:17 
GeneralRe: problems in firefox Pin
arindam.roy5-Nov-07 1:36
arindam.roy5-Nov-07 1:36 
GeneralRe: problems in firefox Pin
Jahangir Shahzad5-Nov-07 17:16
Jahangir Shahzad5-Nov-07 17:16 
GeneralRe: problems in firefox Pin
udhayaprakash15-Dec-08 16:00
udhayaprakash15-Dec-08 16:00 
GeneralRe: problems in firefox Pin
rushi_gavankar15-Mar-09 3:26
rushi_gavankar15-Mar-09 3:26 
GeneralGood One!!! Pin
arindam.roy2-Nov-07 9:51
arindam.roy2-Nov-07 9:51 
Questionhow can I send to multi-user Pin
margiex31-Jul-07 19:43
margiex31-Jul-07 19:43 
like a chat room?
AnswerRe: how can I send to multi-user Pin
Phuongkar1-Aug-07 4:41
Phuongkar1-Aug-07 4:41 
Generalslowness Pin
RichElswick31-Jul-07 2:37
RichElswick31-Jul-07 2:37 
GeneralRe: slowness Pin
Phuongkar1-Aug-07 4:48
Phuongkar1-Aug-07 4:48 
GeneralRe: slowness Pin
RichElswick1-Aug-07 5:39
RichElswick1-Aug-07 5:39 
QuestionWhy using arrays? Pin
Polity4h28-Jul-07 14:08
Polity4h28-Jul-07 14:08 
AnswerRe: Why using arrays? Pin
Phuongkar28-Jul-07 19:17
Phuongkar28-Jul-07 19:17 
GeneralRe: Why using arrays? Pin
Polity4h29-Jul-07 10:05
Polity4h29-Jul-07 10:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.