|
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Ajax;
namespace AJAXTest
{
/// <summary>
/// Summary description for BubisChat.
/// </summary>
public class BubisChat : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtName;
protected System.Web.UI.WebControls.TextBox txtInput;
protected System.Web.UI.WebControls.Button btnSend;
protected System.Web.UI.WebControls.ListBox lstNames;
protected System.Web.UI.WebControls.TextBox txtMsg;
protected System.Web.UI.WebControls.Button btnChangeName;
protected System.Web.UI.WebControls.Button btnClear;
protected System.Web.UI.WebControls.LinkButton lnkRefresh;
private BubisChatData chatData;
private string currentName
{
get { return (string) chatData.names[Session.SessionID]; }
}
private void Page_Load(object sender, System.EventArgs e)
{
chatData = (BubisChatData) Cache["bubis_chat_data"];
if (chatData == null)
{
chatData = new BubisChatData();
Cache.Add("bubis_chat_data", chatData, null, DateTime.Now.AddDays(1),
System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
}
txtMsg.Text = chatData.msgText.ToString();
string name = currentName;
if (name == null)
{
name = txtName.Text;
int i = 1;
while (!AddName(name + i))
i++;
name += i;
AddName(name);
}
if (!IsPostBack)
txtName.Text = name;
ShowNames();
PutSessionIDInCache();
}
private void PutSessionIDInCache()
{
string key = "bubischat_sessionid_" + Session.SessionID;
Context.Cache.Add(key, Session.SessionID, null, DateTime.Now.AddMinutes(Context.Session.Timeout),
System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, new System.Web.Caching.CacheItemRemovedCallback(chatData.RemovedCallback));
}
private bool AddName(string name)
{
name = name.Trim();
if (name == "" || chatData.names.ContainsValue(name))
return false;
chatData.names.Add (Session.SessionID, name);
return true;
}
private void ChangeName(string newname)
{
chatData.names.Remove (Session.SessionID);
AddName (newname.Trim());
}
private void ShowNames()
{
lstNames.Items.Clear();
SortedList list = new SortedList();
foreach (string name in chatData.names.Values)
list.Add (name, null);
foreach (string name in list.Keys)
lstNames.Items.Add (name);
}
#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.btnClear.Click += new System.EventHandler(this.btnClear_Click);
this.btnChangeName.Click += new System.EventHandler(this.btnChangeName_Click);
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnSend_Click(object sender, System.EventArgs e)
{
if (txtInput.Text.Trim() != "")
{
if (chatData.msgText.Length > 10000)
chatData.msgText.Remove (0, 3000);
chatData.msgText.Append (currentName + ": " + txtInput.Text + "\r\n\r\n");
txtMsg.Text = chatData.msgText.ToString();
txtInput.Text = "";
}
}
private void btnChangeName_Click(object sender, System.EventArgs e)
{
string name = txtName.Text.Trim();
if (name == "")
{
Response.Write ("Type a name in the box.");
return;
}
if (chatData.names.ContainsValue(name))
{
Response.Write ("This name already exists in the list.");
return;
}
chatData.msgText.Append (String.Format("------ {0} changed his name to '{1}' -------\r\n\r\n", currentName, name));
txtMsg.Text = chatData.msgText.ToString();
ChangeName(name);
ShowNames();
}
private void btnClear_Click(object sender, System.EventArgs e)
{
chatData.msgText.Length = 0;
txtMsg.Text = "";
}
public class BubisChatData
{
public System.Text.StringBuilder msgText = new System.Text.StringBuilder();
public Hashtable names = new Hashtable();
public void RemovedCallback(String k, Object sessionID, System.Web.Caching.CacheItemRemovedReason r)
{
names.Remove (sessionID);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.