zoukankan      html  css  js  c++  java
  • asp.net2.0+ajax开发的无刷新聊天室Demo【转】

    其实很简单,就是利用ajax中updatepanel实现无刷新显示
    然后利用timer实现重复的调用
    application保存聊天信息
    session保存登陆人信息

    由于2个小时就弄完了,没有完全考虑
    所以,资源浪费很严重,只能运用于轻量级的小型聊天。

    也算是我对聊天室的一些理解

    其实我个人认为聊天室的关键技术在于如何使用媒介来保存所有用户的公有信息
    有的聊天系统将用户发送的信息发送到数据库,然后刷新的去读取数据库
    这样效率太低下
    有的利用文本
    有的利用application来存储
    当然还有别的(毕竟我是菜鸟,才学asp.net一个月)

    我个人比较青睐application保存,然后一定数量后保存到文本或者数据库中
    或者利用队列的方式,先进来的信息,到达数据条数到达设定的极限的时候,先进先踢
    保持数据的显示量和最新。

    至于如何实现,分聊天室,提供头像,提供私聊等,都可以利用这些粗糙的技术加以雕琢。

    大致代码如下,Demo的原代码在附件中

    登陆的时候

        protected void Button1_Click(object sender, EventArgs e)

        {

            if (TextBox1.Text == "")

                return;

            else

                Session["Name"] = TextBox1.Text;

            if (Application["TalkContent"] == null)

                Application["TalkContent"] = "欢迎"+ Session["Name"]+"的到来!<br>";

            else

                Application["TalkContent"] += "欢迎" + Session["Name"] + "进来了!<br>";

     

            Page.Response.Redirect("~/Talk.aspx");

    聊天的地方

        protected void Page_Load(object sender, EventArgs e)

        {

            //Application["TalkContent"] = "Hello Gary!";

            Label1.Text = Application["TalkContent"].ToString ();

            //Application["TalkContent"] = "";

            Page.Title = "Hello " + Session["Name"].ToString ()+" WelCome to Talkroom!";

        }

        protected void Button1_Click(object sender, EventArgs e)

        {

            if (TextBox1.Text == "")

                return;

            Application["TalkContent"] += Session["Name"] +" Say: "+ TextBox1.Text + "<br>";

            Label1.Text = Application["TalkContent"].ToString ();

            TextBox1.Text = string.Empty;

        }

        protected void Timer1_Tick(object sender, EventArgs e)

        {

            Label1.Text = Application["TalkContent"].ToString();

            //

            // 到一定的限度,清屏

            // todo:可以将原有信息保存下来

            if (Application["TalkContent"].ToString().Length > 2000)

                Application["TalkContent"] = "";

     

        }

    关于聊天内容无刷新的实现

            <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">

                <ContentTemplate>

                    &nbsp;&nbsp; &nbsp;

                    &nbsp; &nbsp;

                   <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 

                    <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">

                       

                    </asp:Timer>

                    &nbsp;&nbsp;

                </ContentTemplate>

                <Triggers>

                    <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />

                </Triggers>

    提交信息的无刷新实现

            <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">

                <ContentTemplate>

                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

     

                </ContentTemplate>

                <Triggers>

                    <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />

                </Triggers>

            </asp:UpdatePanel>

  • 相关阅读:
    xtrabackup备份原理
    四“当”
    MySQL BinLog Server 搭建实战
    mysqldump 原理
    MGR测试及搭建
    自动化测试-12.selenium的弹出框处理
    自动化测试-11.selenium的下拉框处理类Select
    自动化测试-10.selenium的iframe与Frame
    自动化测试-9.selenium多窗口句柄的切换
    自动化测试-8.selenium操作元素之键盘和鼠标事件
  • 原文地址:https://www.cnblogs.com/Godblessyou/p/1779199.html
Copyright © 2011-2022 走看看