|
前些日子,在网上看到好多WEB网站即时消息的例子,大都是XML做的,无奈对于我这个跑的慢的人(XML看了一个月,还是没搞明白怎么用 ),只好望而兴叹了。 没办法只能从C#的角落里把Application再揪出来用用了.(尽管听他们说实现的效率比WebService差很多) 。呵呵,废话不说了,不过在高手面前班门弄斧还是有点脸红的,呵呵. (1) 存入用户列表 把用户名存入Application 代码如下: string User=Server.UrlDecode(Request.QueryString["User"]); Session["UserName"]=User; Application["OnlineUser"]+=Session["UserName"].ToString()+","; Response.Write("<SCRIPT>window.open('index.aspx','','left=800,scrollbars=yes,width=200,height=500');</SCRIPT>"); (2) 建框架页(index.aspx) 10秒钟自动刷新 TOPframe页 隐藏该框架,负责弹出消息框 Application[“message”]为客户留言内容,在发送留言页面对其赋值; UserNameList提取接收人变量; 代码如下: try { if(Application["message"]!="") { string UserNameList=this.Application["message"].ToString().Substring(this.Application["message"].ToString().IndexOf("对")+1,(this.Application["message"].ToString().IndexOf("说")-this.Application["message"].ToString().IndexOf("对")-1)); if (Session["UserName"].ToString()==UserNameList) { Response.Write("<SCRIPT>window.open('messagelist.aspx','','left=400,top=400,scrollbars=yes,width=400,height=300');</SCRIPT>"); } } } catch {} mainframe页: 显示客服人员列表: 注意:在这里要判断一下,如果是客服人员则显示. 如果是客户则不显示,客户记录Session["UserName"]时,用当前时间+随机数.在客户点击相应客服人员时把其存入Application用户列表,在这里不多叙述. 代码如下: string [] abc=Application["OnlineUser"].ToString().Split(','); Label1.Text="在线人数列表:<br>"; for(int i=0;i<abc.Length;i++) { string UserList=abc[i].ToString(); Label1.Text+="<a href=# onclick=window.open('send.aspx?User="+UserList+"','','left=400,top=400,scrollbars=yes,width=400,height=200');>"+abc[i].ToString()+"</a><br>"; } (3) 客户向客服发送消息: 此页与TOPFRAME页构成一收一发. Application["message"]=Session["UserName"]+"对"+User+"说:<br>"+message.Text; Response.Write("<script>alert('发送成功!');window.close();</script>"); (4)接受消息与回复(messagelist.aspx) 显示接收消息后,把消息清空 if(!Page.IsPostBack) { Label1.Text=Application["message"].ToString(); User1=Application["message"].ToString().Substring(0,Application["message"].ToString().IndexOf("对")); Label2.Text=User1; Application["message"]=""; } 回复发送人: string User111=Label2.Text; Application["message"]=Session["UserName"]+"对"+User111+"说:<br>"+message.Text; Response.Write("<script>alert('发送成功!');window.close();</script>"); 以上仅供为大家提供一个思路,还望高人多多指点.
|