简体   繁体   English

Facebook风格ASP.NET聊天组件

[英]Facebook style ASP.NET chat component

I will be launching a site that is somewhat like a social media site.I need a ASP.NET chat control which has to be AJAX based and it will be nice to have jQuery as my entire site will be themed using jQuery Themes. I will be launching a site that is somewhat like a social media site.I need a ASP.NET chat control which has to be AJAX based and it will be nice to have jQuery as my entire site will be themed using jQuery Themes. What i am looking for is something similar to the Gmail or facebook style chat as that is very easy to use from the users point of view and does not take a lot of screen real estate.我正在寻找的是类似于 Gmail 或 facebook 风格的聊天,因为从用户的角度来看,它非常易于使用,并且不会占用大量屏幕空间。

Any thoughts here no what i can find.这里的任何想法都没有我能找到的。 I have looked all over google and have not been able to find anything like that for ASP.NET.我在谷歌上找遍了所有的地方,都找不到像 ASP.NET 这样的东西。 There are many out there for Php that i see.我看到的 Php 有很多。 Has anyone worked on this before?有没有人在这之前工作过? We want to launch the site in June so i have to find something quick.我们想在 6 月推出该网站,所以我必须尽快找到一些东西。 Appreciate the help.感谢帮助。

try this.. Sample Image - SimpleChat.jpg Introduction试试这个.. 示例图片 - SimpleChat.jpg 简介

And why not, how to create an easy chat room for your web site?为什么不呢,如何为您的 web 站点创建一个简单的聊天室? Well, the best way is to use a nice database to store messages;好吧,最好的方法是使用一个好的数据库来存储消息; however, for demo purposes, I'll use a static array.但是,出于演示目的,我将使用 static 阵列。 I know, you won't be able to use it in your web farm.我知道,您将无法在 web 场中使用它。 Take this article as the concept, not as a solution.把这篇文章当作概念,而不是解决方案。 This simple web chat program is intended to work in any browser supporting.这个简单的 web 聊天程序适用于任何支持的浏览器。

Also, you can select multiple chat rooms.此外,您可以 select 多个聊天室。 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.几个月前,我正在寻找一个完整的在线客服ASP.NET控件,以使我的生活更轻松,没有发现任何有趣的东西,所以我自己建立了。 Using the code使用代码

Replace this class if you are using a database to save the messages: Collapse如果您使用数据库来保存消息,请替换此 class:折叠

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.上面的代码从 static 数组中读取和写入,就像在数据库中一样。 The code only allows having 200 messages in the array, after that it deletes the top 10 at the time.该代码只允许在数组中包含 200 条消息,之后它会删除当时的前 10 条消息。

The Chat page is pretty simple;聊天页面非常简单; this is the code behind aspx.cs: Collapse这是 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.单击 SEND 按钮时,它会调用 function AddMessage 将一行添加到 static 数组的末尾。

The page inside the tag refreshes every 4 seconds without refreshing your actual page.标签内的页面每 4 秒刷新一次,而不刷新您的实际页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM