Simple chat application for ASP.NET

原创 2004年09月04日 18:42:00

Sample Image - SimpleChat.jpg

Introduction

And why not, how to create an easy chat room for your web site? Well, the best way is to use a nice database to store messages; however, for demo purposes, I'll use a static array. I know, you won't be able to use it in your web farm. Take this article as the concept, not as a solution. This simple web chat program is intended to work in any browser supporting <iFrame>.

Also, you can select multiple chat rooms. Why not extend from there and more from channel to channel.

Background

Some months ago, I was looking for a complete on-line customer service ASP.NET control to make my life easier, did not find anything interesting, so I built my own.

Using the code

Replace this class if you are using a database to save the messages:

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);
        }
    }

The above code reads and writes from the static array like in a database. The code only allows having 200 messages in the array, after that it deletes the top 10 at the time.

The Chat page is pretty simple; this is the code behind aspx.cs:

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 = "";        
            }
        }
    }

When the SEND button is clicked, it calls the function AddMessage that adds a row into the end of the static array.

The page inside the <iframe> tag refreshes every 4 seconds without refreshing your actual page.

Points of Interest

The magic? None, a simple request of the second page into the <iFrame>. So, Internet Explorer takes care of everything for us to read the static array.

Asp_net_ChatRoom_邹华栋.pptx

  • 2014年01月04日 19:56
  • 479KB
  • 下载

从一个简单的聊天程序SimpleChat看VPN技术

SimpleVPN写好了以后,感觉比较简单,我觉得只有简单的东西才经得起折腾,才能全民折腾,所以说SimpleVPN还不够简单,本文来一个更加简单的,展示一个超级简单的点对点聊天程序,而且还带简单加密...
  • dog250
  • dog250
  • 2017-05-01 23:41:58
  • 6515

SimpleAjaxChat.rar

  • 2011年12月15日 14:32
  • 38KB
  • 下载

ASP.NET实现简单的聊天室,主要是Application对象

用ASP.NET做一个最简单的聊天室,逻辑清晰简单,能帮你了解Application的用法。 第一步我们先构想我们需要的页面,应该也就是两个吧,第一个登陆页面Login.aspx,第二个聊天室主页面...
  • u011955860
  • u011955860
  • 2013-12-27 10:01:47
  • 3265

ASP.NET AJAX web chat application

  • 2014年07月01日 10:54
  • 302KB
  • 下载

Chat application

  • 2012年11月28日 15:31
  • 10KB
  • 下载

ASP.Net Application的用法

1.Application的创建 Application["application名称"] = "application的值"; 2.Application的使用 string str = ...
  • gongth_12
  • gongth_12
  • 2013-06-13 16:04:53
  • 4523

Win32 Application Hello World 详细注释

// win32project.cpp : Defines the entry point for the application. // #include "stdafx.h"...
  • Dextrad_ihacker
  • Dextrad_ihacker
  • 2016-12-22 22:31:15
  • 741

ASP.NET中static 变量和Application的区别

在C#中,static 变量不属于类的实例,也就是说该类的所有实例共享一个static变量ASP.NET页面就是一个类,那么一个用户访问一个页面就是NEW一个类,那么和APPLICATION有什么去区...
  • ccp5780199
  • ccp5780199
  • 2007-08-21 01:11:00
  • 1831
收藏助手
不良信息举报
您举报文章:Simple chat application for ASP.NET
举报原因:
原因补充:

(最多只允许输入30个字)