C#实例之简单聊天室(状态管理)
前言
状态管理是在同一页或不同页的多个请求发生时,维护状态和页信息的过程。因为Web应用程序的通信协议使用了无状态的HTTP协议,所以当客户端请求页面时,ASP.NET服务器端都会重新生成一个网页实例。此时,旧网页的任务完成,旧网页的实例也随之消失。这种无状态,意味着客户端用户在浏览器中的一些状态或是对数据的一些修改都将丢失。
简单聊天室
这里运用System.Web命名空间的那些管理状态的类,做一个简单的聊天室
protected void Button1_Click(object sender, EventArgs e){Application.Lock();int intUserNum;string user;string[] users;string strUserName;intUserNum = Convert.ToInt32(Application["userNum"]);if (intUserNum > 20){Response.Write("在线人数超过20人,不能登录了!");Response.Redirect("Default.aspx");}else {user = Application["user"].ToString();users = user.Split(',');strUserName = TextBox1.Text.Trim();for (int i = 0; i < intUserNum; i++) {if (users[i] == strUserName){Response.Redirect("Default.aspx?value=1");}}if (users == null)Application["user"] = strUserName;elseApplication["user"] += "," + strUserName;intUserNum += 1;Application["userNum"] = intUserNum.ToString();Session["user"] = strUserName;Application.UnLock();Response.Redirect("main.aspx"); ;}}
public partial class Left : System.Web.UI.Page{private ArrayList ItemList = new ArrayList();protected void Page_Load(object sender, EventArgs e){Application.Lock();if (Session["user"] == null)Response.Redirect("Default.aspx");elseLabel1.Text = Session["user"].ToString();int userCount = int.Parse(Application["userNum"].ToString());Label2.Text = userCount.ToString();string user = Application["user"].ToString();string[] users = user.Split(',');for (int i = (userCount-1); i >= 0; i--) {ItemList.Add(users[i]);}ListBox1.DataSource = ItemList;ListBox1.DataBind();Application.UnLock();}}
protected void Page_Load(object sender, EventArgs e){Application.Lock();if (!IsPostBack) {string chat=Application["chats"].ToString();int chatNum=int.Parse(Application["current"].ToString()) ;string[] chats = chat.Split(',');for (int i = chatNum - 1; i >= 0; i--){if(chatNum==0)TextBox1.Text=chats[i];elseTextBox1.Text=TextBox1.Text+"\n"+chats[i];}}Application.UnLock();}
public partial class bottom : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){Application.Lock();string userName = Application["user"].ToString();string[] users = userName.Split(',');DropDownList1.DataSource = users;DropDownList1.DataBind();Application.UnLock();}protected void Button1_Click(object sender, EventArgs e){Application.Lock();int current = int.Parse(Application["current"].ToString());string liaotian = Session["user"] + "对" + DropDownList1.SelectedValue.ToString() + "说:" + TextBox1.Text.Trim() + DateTime.Now.ToString();if (current == 0 || current > 40){current = 0;Application["chats"] = liaotian;}else{Application["chats"] +=","+ liaotian;}current += 1;Application["current"] = (object)current;Application.UnLock();}}
<div><iframe id="ifraLeft" name="ifraLeft" src="Left.aspx"style="width:200px;height:500px;float:left;margin-left:10px;margin-top:10px;background-color:grey;border:2px solid #000000"></iframe><iframe id="ifraRight" name="ifraRight" src="Right.aspx"style="width:700px;height:500px;float:left;margin-top:10px;background-color:#000000;border:2px solid #000000;"></iframe><iframe id="ifrabottom" name="ifrabottom" src="bottom.aspx"style="width:900px;height:50px;margin-left:10px;margin-top:10px;background-color:red;border:2px solid #000000;"></iframe></div>
案例代码下载
C#实例之简单聊天室(状态管理)的更多相关文章
- java web利用mvc结构实现简单聊天室功能
简单聊天室采用各种内部对象不适用数据库实现. 一个聊天室要实现的基本功能是: 1.用户登录进入聊天室, 2.用户发言 3.用户可以看见别人发言 刚才算是简单的需求分析了,现在就应该是进 ...
- 基于Server-Sent Event的简单聊天室 Web 2.0时代,即时通信已经成为必不可少的网站功能,那实现Web即时通信的机制有哪些呢?在这门项目课中我们将一一介绍。最后我们将会实现一个基于Server-Sent Event和Flask简单的在线聊天室。
基于Server-Sent Event的简单聊天室 Web 2.0时代,即时通信已经成为必不可少的网站功能,那实现Web即时通信的机制有哪些呢?在这门项目课中我们将一一介绍.最后我们将会实现一个基于S ...
- Python Socket 简单聊天室2
上篇文章写了一个简单的单线程的一问一答的简单聊天室.这次我们使用SocketServer模块搭建一个多线程异步的聊天室. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- Asp.Net SignalR - 简单聊天室实现
简单聊天室 使用持久链接类我们就可以做一些即时通讯的应用了,我使用Group做了一个简单的聊天室,先上图技术细节下面再讲 可以加入聊天室.创建聊天室.发送消息,下面就说说我是如何通过Group做出来的 ...
- SpringBoot 搭建简单聊天室
SpringBoot 搭建简单聊天室(queue 点对点) 1.引用 SpringBoot 搭建 WebSocket 链接 https://www.cnblogs.com/yi1036943655/p ...
- ASP.NET SingalR + MongoDB 实现简单聊天室(一):搭建基本框架
ASP.NET SingalR不多介绍.让我介绍不如看官网,我这里就是直接上源代码,当然代码还是写的比较简单的,考虑的也少,希望各位技友多多提意见. 先简单介绍聊天室功能: 用户加入聊天室,自动给用户 ...
- 利用socket.io+nodejs打造简单聊天室
代码地址如下:http://www.demodashi.com/demo/11579.html 界面展示: 首先展示demo的结果界面,只是简单消息的发送和接收,包括发送文字和发送图片. ws说明: ...
- C++ socket 网络编程 简单聊天室
操作系统里的进程通讯方式有6种:(有名/匿名)管道.信号.消息队列.信号量.内存(最快).套接字(最常用),这里我们来介绍用socket来实现进程通讯. 1.简单实现一个单向发送与接收 这是套接字的工 ...
- C#基于Socket的简单聊天室实践
序:实现一个基于Socket的简易的聊天室,实现的思路如下: 程序的结构:多个客户端+一个服务端,客户端都是向服务端发送消息,然后服务端转发给所有的客户端,这样形成一个简单的聊天室功能. 实现的细节: ...
随机推荐
- To be contine ,NW NMM backup sqlserver failed.
Last time, we talk about separate under one cluster backup into two diffetent AG backup. Does it wor ...
- 全网一定不是最好懂的C++线性筛素数
Part 0:概念 先给几个概念(很重要): 合数:如果\(xy=z\text{且}x,y\text{为正整数}\),我们就说\(x,y\text{是}z\text{的合数}\) 素数:如果数\(a\ ...
- Codeforces Round #617 (Div. 3) D. Fight with Monsters
D : Fight with Monsters 题目大意 : 有一组数,每个值对应着一个怪物的 hp 值,现在有两个人,一个自己一个对手,每个人有一个攻击值, 两个人轮流攻击怪物,如果是自己将怪物先打 ...
- 在centos6.3下安装php的Xdebug
首先下载一个xdebug http://www.xdebug.org/docs/ 官网上有windos版本和linux源码版本的,我们下载一个源码包xdebug-2.2.5.tgz 然后进入安装流程 ...
- Yandex Big Data Essentials Week1 Unix Command Line Interface Processes managing
free displays the total amount of free and used memory free [options] top provides a dynamic real-ti ...
- Spring中的可扩展接口
1.监听器Listener(点此连接,执行流程带源码分析及demo) 2.bean定义的后置处理器(BeanDefinitionRegistryPostProcessor)和bean工厂的后置处理器( ...
- 13-MyBatis03(逆向工程)
MyBatis逆向工程 1.导入jar包 <dependency> <groupId>org.mybatis</groupId> <artifactId> ...
- php面试笔记(4)-php基础知识-流程控制
本文是根据慕课网Jason老师的课程进行的PHP面试知识点总结和升华,如有侵权请联系我进行删除,email:guoyugygy@163.com 在面试中,考官往往喜欢基础扎实的面试者,而流程控制相关的 ...
- 管理 使用 FastDFS
启动管理tracker: 1. 启动文件+配置文件+命令 /usr/bin/fdfs_trackerd <config_file> [start | stop | restart] 举例: ...
- expect知识梳理
1 expect expect软件用于实现非交互式操作,实际应用中常用于批量部署,可以帮助运维人员管理成千上万台服务器. expect实现非交互式操作主要是在程序发出交互式询问时,按条件传递程序所需的 ...