이 .. 샘플 이미지를 시도 - SimpleChat.jpg 소개
그리고 왜, 어떻게 당신의 웹 사이트를위한 쉬운 채팅방을 만드는 방법하지? 글쎄, 가장 좋은 방법은 좋은 데이터베이스를 사용하여 메시지를 저장하는 것입니다. 그러나 데모 목적으로 정적 배열을 사용하겠습니다. 나도 알아, 당신은 당신의 웹 농장에서 그것을 사용할 수 없을 것입니다. 이 기사를 솔루션이 아니라 개념으로 생각하십시오. 이 간단한 웹 채팅 프로그램은 지원하는 모든 브라우저에서 작동하도록되어 있습니다.
또한 여러 채팅방을 선택할 수 있습니다. 왜 그곳에서 채널로 확장하지 않을까요? 배경
몇 달 전, 전 온라인 고객 서비스 ASP.NET 컨트롤을 사용하여 내 인생을 더 쉽게 만들고 재미있는 것을 찾지 못했습니다. 그래서 내 자신을 만들었습니다. 당신이 메시지를 저장하는 데이터베이스를 사용하는 경우 코드
를 사용하면이 클래스를 대체합니다 축소
public class Chat
{
static protected ArrayList pArray = new ArrayList();
static public void AddMessage(string sDealer,
string sUser, string sMsg)
{
string sAddText = sDealer + "~" + sUser + "~" + sMsg;
pArray.Add(sAddText);
if (pArray.Count > 200)
{
pArray.RemoveRange(0,10);
}
}
static public string GetAllMessages(string sDealer)
{
string sResponse = "";
for (int i=0; i< pArray.Count; i++)
{
sResponse = sResponse +
FormatChat(pArray[i].ToString(), sDealer);
}
return(sResponse);
}
static private string FormatChat(string sLine, string sDealer)
{
int iFirst = sLine.IndexOf("~");
int iLast = sLine.LastIndexOf("~");
string sDeal = sLine.Substring(0, iFirst);
if (sDeal != sDealer)
return("");
string sUser = sLine.Substring(iFirst+1, iLast-(iFirst+1));
string sMsg = sLine.Substring(iLast+1);
string sRet = "" + sUser + ": " + sMsg + "";
return(sRet);
}
}
위의 코드 읽기 및 데이터베이스에 같은 정적 배열에서 기록합니다. 이 코드는 배열에서 200 개의 메시지 만 허용하고, 그 다음에는 상위 10 개를 삭제합니다.
채팅 페이지는 매우 간단합니다. 축소
public class ChatWin : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TB_ToSend;
protected System.Web.UI.WebControls.Button BT_Send;
private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack == false)
{
if (Request.Params["Channel"] != null)
Session["ChatChannel"] =
Request.Params["Channel"].ToString();
else
Session["ChatChannel"] = "1";
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <SUMMARY>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </SUMMARY>
private void InitializeComponent()
{
this.BT_Send.Click +=
new System.EventHandler(this.BT_Send_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public string GetChatPage()
{
return("TheChatScreenWin.aspx");
}
private void BT_Send_Click(object sender, System.EventArgs e)
{
string sChannel = "";
string sUser = "";
if (Request.Params["Channel"] != null)
sChannel = Request.Params["Channel"].ToString();
else
sChannel = "1";
if (Request.Params["User"] != null)
sUser = Request.Params["User"].ToString();
else
{
Random pRan = new Random();
int iNum = pRan.Next(9);
sUser = "Annonymouse" + iNum;
}
if (TB_ToSend.Text.Length > 0)
{
PageModule.Chat.AddMessage(sChannel,
sUser,
TB_ToSend.Text);
TB_ToSend.Text = "";
}
}
}
송신 버튼을 클릭하면, 정적 배열의 단부에 행을 추가 기능 AddMessage 호출이 aspx.cs 뒤에 코드이다.
실제 페이지를 새로 고치지 않고 태그 안의 페이지가 4 초마다 새로 고침됩니다.
codeplex에서 검색하면 더 많은 정보를 찾을 수 있습니다. http://www.codeplex.com/site/search?query=chat&ac=3 –