吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5493|回复: 23
收起左侧

[其他转载] 【C#】制作聊天室(Application+Session+Response+Request的使用)

  [复制链接]
夏沫梦影 发表于 2019-10-10 19:32
本帖最后由 夏沫梦影 于 2019-10-10 19:34 编辑


Application对象的典型应用:聊天室的制作

软件:Visual Studio 2015

练习:Application+Session+Response+Request

示例:

主要步骤:
1.新建一个网页,并命名为Login.aspx(并将其设置为起始页)

示例:

Login.aspx页面:创建一个3行2列的表格距地代码如下:
[C#] 纯文本查看 复制代码
<table align="center" border="1" cellpadding="0" cellspacing="0" style="width:270px;height:150px">
        <tr>
            <td colspan="2" style="font-weight:bold;font-size:16pt;color:#4cff00;background-color:#54a4ff;text-align:center">登录</td>
        </tr>
                <tr>
            <td style="text-align:left;font-size:11pt;background-color:#4094c4">用户名:</td>
            <td style="background-color:#4094c4;text-align:left;">
                <asp:TextBox ID="txt_UserName" runat="server"></asp:TextBox>
            </td>
        </tr>
                <tr>
            <td colspan="2" style="background-color:#54a4ff;text-align:center;">
                <asp:Button ID="btn_Login" runat="server" Text="登录" />  
                <asp:Button ID="btn_Quit" runat="server" Text="退出" />
            </td>
        </tr>
    </table>

Login.aspx.cs页面: 页面载入事件中添加一个判断,判断该用户是否已登录。具体如下:
[C#] 纯文本查看 复制代码
 protected void Page_Load(object sender, EventArgs e)
    {
        int P_int_judge = 0;
        P_int_judge = Convert.ToInt32(Request["value"]);
        if (!IsPostBack)
        {
            if (P_int_judge == 1)
            {
                Response.Write("<script>alert('该用户已登录!')</script>");
            }
        }
    }

登录按钮事件:
[C#] 纯文本查看 复制代码
protected void btn_Login_Click(object sender, EventArgs e)
    {
        Application.Lock();
        int P_int_num; //在线人数
        string P_str_name;//登录用户
        string P_str_names;//已在线用户名
        string[] P_str_user;//用户在线数组
        P_int_num = Convert.ToInt32(Application["userNum"].ToString());
        if (txt_UserName.Text.Trim() == "")//非空验证
        {
            Response.Write("<script>alert('用户名不得为空')</script>");
        }
        else
        {
            //判断登录者是否已在线
            P_str_name = txt_UserName.Text.Trim();
            P_str_names = Application["user"].ToString();
            P_str_user = P_str_names.Split(',');
            for (int i = 0; i < P_int_num - 1; i++)
            {
                if (P_str_name == P_str_user[i].Trim())
                {
                    int P_int_judge = 1;
                    Response.Redirect("Login.aspx?value=" + P_int_judge);
                }
            }
            if (P_int_num == 0)//将登录者加入在线用户列表的Applicaion中
            {
                Application["user"] = P_str_name.ToString();
            }
            else
            {
                Application["user"] = Application["user"] + "," + P_str_name.ToString();
            }
            //将在线用户数量+1
            P_int_num += 1;
            Application["userNum"] = P_int_num;
            Session["userName"] = txt_UserName.Text.Trim();//将登录者信息存入Session中
        }
      
            Application.UnLock();
        Response.Redirect("Default.aspx");
    }

退出按钮事件:
[C#] 纯文本查看 复制代码
protected void btn_Quit_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.close();</script>");
    }

2.新建一个全局变量:Global.aspx
开始事件中添加如下代码:
[C#] 纯文本查看 复制代码
void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
        //建立用户列表
        string user = "";    //用户列表
        Application["user"] = user;
        Application["userNum"] = 0;
        string chats="";     //聊天记录
        Application["chats"] = chats;
        //记录当前聊天数
        Application["current"] = 0;
    }

结束事件中添加如下代码:
[C#] 纯文本查看 复制代码
 void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown
        Application["user"] = "";
        Application["chats"] = "";
    }

3.新建一个网页,用默认名称Default.aspx

示例:

Default.aspx页面

创建一个4行2列的表格:具体代码如下:

[C#] 纯文本查看 复制代码
<div style="text-align:center">
    <table align="center" style="width:603px;height:442px" border="1" cellpadding="0" cellspacing="0">
        <tr>
            <td colspan="2" style="height:51px;font-size:16pt;color:#ffffff;background-color:#808080">软件开发聊天室</td>
        </tr>
        <tr>
            <td style="width:404px;height:18px">聊天信息</td>
            <td align="left" style="width:200px;height:18px;font-size:11pt;border-right-style:none;border-left-style:none" >用户列表</td>
        </tr>
        <tr>
            <td style="width:404px;height:354px">
                <iframe id="iframe1" src="Content.aspx" runat="server" scrolling="no" noresize="noResize" frameborder="0" style="width: 400px;height: 350px"></iframe>
            </td>
            <td style="width:200px;height:354px">
                <iframe id="iframe2" src="List.aspx" runat="server" scrolling="no" noresize="noResize" style="width:200px;height:350px"></iframe>
            </td>
        </tr>
        <tr>
            <td style="width:400px" align="left">
                <asp:TextBox ID="txt_Message" Width="310px" runat="server"></asp:TextBox>
                <asp:Button ID="btn_Send" runat="server" Text="发言" />
            </td>
            <td>
                <asp:Button ID="btn_Quit" runat="server" Text="退出聊天室" />
            </td>
        </tr>
    </table> 
    </div>

Default.aspx.cs页面

发送按钮点击事件:

[C#] 纯文本查看 复制代码
 protected void btn_Send_Click(object sender, EventArgs e)
    {
        int P_int_current = Convert.ToInt32(Application["current"]);
        Application.Lock();
        if (P_int_current == 0 || P_int_current > 20)
        {
            P_int_current = 0;
            Application["chats"] = Session["userName"].ToString() + "说:" + txt_Message.Text.Trim() + "(" + DateTime.Now.ToString() + ")";
        }
        else
        {
            Application["chats"] = Application["chats"].ToString() + "," + Session["userName"].ToString() + "说:" + txt_Message.Text.Trim() + "(" + DateTime.Now.ToString() + ")";
        }
        P_int_current += 1;
        Application["current"] = P_int_current;
        Application.UnLock();
        txt_Message.Text = "";
        txt_Message.Focus();
    }

退出按钮点击事件:

[C#] 纯文本查看 复制代码
 protected void btn_Quit_Click(object sender, EventArgs e)
    {
        Application.Lock();
        string P_str_userName = Application["user"].ToString();
        Application["user"] = P_str_userName.Replace(Session["userName"].ToString(), "");
        Application.UnLock();
        Response.Write("<script>window.close();</script>");
    }

4.新建一个网页,命名为Content.aspx(用来显示聊天信息)

Content.aspx页面

添加一个文本框,改为多行显示:

[C#] 纯文本查看 复制代码
<div>
        <asp:TextBox ID="txt_Content" runat="server" Height="345px" TextMode="MultiLine" Width="380px"></asp:TextBox>
    </div>

Content.aspx.cs页面

页面载入事件中添加如下代码:

[C#] 纯文本查看 复制代码
 protected void Page_Load(object sender, EventArgs e)
    {
        int P_int_current = Convert.ToInt32(Application["current"]);
        Application.Lock();
        string P_str_chats = Application["chats"].ToString();
        string[] P_str_chat = P_str_chats.Split(',');
        for (int i = P_str_chat.Length - 1; i >= 0; i--)
        {
            if (P_int_current == 0)
            {
                txt_Content.Text = P_str_chat[i].ToString();
            }
            else
            {
                txt_Content.Text = txt_Content.Text + "\n" + P_str_chat[i].ToString();
            }
        }
        Application.UnLock();
    }

5.新建一个网页,命名为List.aspx(用来显示用户信息)

List.aspx页面

添加一个listbox,具体如下:

[C#] 纯文本查看 复制代码
<div>
        <asp:ListBox ID="lb_User" runat="server" Height="345px" Width="180px"></asp:ListBox>
    </div>

List.aspx.cs页面

页面载入事件中添加如下代码:

[C#] 纯文本查看 复制代码
 protected void Page_Load(object sender, EventArgs e)
    {
        ArrayList ItemList = new ArrayList();
        Application.Lock();
        string  P_str_names;//已在线的用户
        string[] P_str_user;//用户在线数组
        int P_int_num=Convert.ToInt32(Application["userNum"]);
        P_str_names = Application["user"].ToString();
        P_str_user =P_str_names.Split(',');
        for (int i =(P_int_num - 1); i >= 0; i--)
        {
            if (P_str_user[i].ToString()!= "")
            {
                ItemList.Add(P_str_user[i].ToString());
            }
            
        }
        lb_User.DataSource=ItemList;
        lb_User.DataBind();
        Application.UnLock();
    }

免费评分

参与人数 6吾爱币 +8 热心值 +6 收起 理由
czw52pj + 1 + 1 已经处理,感谢您对吾爱破解论坛的支持!
BBYKIKI + 1 + 1 谢谢@Thanks!
上官流云 + 1 + 1 用心讨论,共获提升!
temper + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
时光里的一缕风 + 1 + 1 热心回复!
苏紫方璇 + 3 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

a876147772 发表于 2019-10-19 13:00
fangxiansheng 发表于 2019-10-17 15:24
英雄联盟无限视距软件从昨天开始出现了封号,本以为是换肤软件出现了问题,今天解封后试了一局,发现只开无 ...

每年这个时候,赛季末,尽量少用视距噢,等赛季结束就OK辣
fangxiansheng 发表于 2019-10-17 15:24
英雄联盟无限视距软件从昨天开始出现了封号,本以为是换肤软件出现了问题,今天解封后试了一局,发现只开无限视距又给我封了,望检查解决

skoa 发表于 2019-10-10 19:51
15774211127 发表于 2019-10-10 20:08
&#128591;感谢分享,不过这个页面丑得我一激灵
 楼主| 夏沫梦影 发表于 2019-10-10 20:11
15774211127 发表于 2019-10-10 20:08
&#128591;感谢分享,不过这个页面丑得我一激灵

哈哈,淡定
psx1lin 发表于 2019-10-11 08:15
研究看看
感谢分享
heroic 发表于 2019-10-11 09:38
这页面算是很有特色了
414504394 发表于 2019-10-18 20:46
怎么不更新视距拉
 楼主| 夏沫梦影 发表于 2019-10-18 21:33
414504394 发表于 2019-10-18 20:46
怎么不更新视距拉

论坛不让发辅助
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-29 07:35

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表