再谈在asp聊天室里实现房间功能与用户的显示 在我写下了《在asp聊天室里实现悄悄话功能》一文后,很多的朋友来信问我 关于其他高级功能实现的问题。确实,例如房间、用户管理等高级功能是一个完善的 聊天室所必有的功能。在这里面又以房间功能是比较难于实现的。因此我想和大家谈谈 这个问题。 我依然倾向于用数组来解决这两个问题,这并不是说不能用数据库来解决,但我们 要考虑性能的问题。而且用数组有几个好处,利于排序,利于控制总的长度。在服务器 内存中长驻(有利于性能的提高)。我先讲一讲房间的问题,这里给出大家在两个系统 房间中切换的方法。至于自建房间的方法,希望大家能触类旁通。还有关于用户在线显示的 问题,因为我们既要显示各个房间的用户和总的用户,所以会和房间产生一些互动,在讲的 时候请大家注意。为了大家使用的方便,我尽量把这些功能写成子程序或子函数,大家可以 直接的应用。 我们先看在gloable.asa中的定义,这很重要 <SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Sub Application_OnStart() application("gRoom_Name")="逍遥游" '保存主聊天室的名字,所有人先到的聊天室
dim pChat_Value(50) application(application("gRoom_Name")&"_Value")=pChat_Value 'p代表private g代表globle 用来保存逍遥游聊天室的内容 application(application("gRoom_Name")&"_Number")=0 '用来保存逍遥游聊天室的谈话的数目 application("养生主"&"_Value")=pChat_Value application("养生主"&"_Number")=0
dim pChater_Value(150) application(application("gRoom_Name")&"er_Value")=pChater_Value '用来保存逍遥游聊天室的聊天者的名字 application(application("gRoom_Name")&"er_Number")=0 '用来表示逍遥游聊天室的上线的人数 application("养生主"&"er_Value")=pChater_Value application("养生主"&"er_Number")=0
dim pRoom_Value(50) pRoom_Value(0)="逍遥游"&"("&"系统"&")"&"["&application(application("gRoom_Name")&"er_Number")&"]"&"人" pRoom_Value(1)="养生主"&"("&"系统"&")"&"["&application("养生主"&"er_Number")&"]"&"人" application("gRoom_Value")=pRoom_Value '用来保存房间的名字 application("gRoom_Number")=2
End Sub
Sub Session_OnStart() session("sRoom_Name")=application("gRoom_Name")'每个新用户的默认房间 session("sChater_Name")="" 's代表session 用来保存说话者的名字 strProvider="Driver={Microsoft Access Driver (*.mdb)}; DBQ="&server.mappath("../chat")&"\"&"db\Chater_Message.mdb;" set Cres=server.createobject("ADODB.Connection") set res=server.createobject("ADODB.Recordset") Cres.open strProvider res.activeconnection=Cres set session("res")=res '一个Recordset的实例 End Sub
在gloable.asa中进行了如上的定义后,我们就可以在后面的页面中应用了。我们假设 先有一个用户的登陆确认(从数据库中确认用户的信息)大致的代码如下: <!-- #include virtual="/chat/inc/Convert.inc" --> <%'将欢迎的内容记入application数组并调整顺序%> <!-- #include virtual="/chat/inc/RoomRefresh.inc" --> <!-- #include virtual="/chat/inc/Logname.inc" --> <%'将姓名记入application数组%> <% if request.servervariables("Request_Method")="POST" and session("sChater_Name")="" then name=request.form("txtName") pass=request.form("txtPass") set res=session("res") sql="Select * From Chater_Message Where Name='"&name&"' And Pass='"&pass&"';" res.open sql,,3,3 if res.BOF then response.write "Login Failed"&"<br>" response.write "<a href="&chr(34)&"LoginPage.htm"&chr(34)&">"&"请重新登陆"&"</a>" res.close else session("sChater_Name")=name Welcome_Message="common"&","&"系统"&","&"所有人"&","&"<font color="&chr(34)&"#ff98ff"&chr(34)&">"&"各位看官,"&session("sChater_Name")&"来也"&"</font>"&"<br>" '这句话请大家一定要注意,如果你读了我的上一篇文章你应该知道是什么意思。 application.lock call convert(Welcome_Message) call logname(session("sChater_Name"),session("sRoom_Name")) call roomrefresh() application.unlock res.close response.redirect "ChatPage.htm" end if end if %>
下面是三个关键的子程序,在后面也有大量的应用。 convert.inc <% sub convert(Message) dim tmpChat_Value() pChat_Value=application(session("sRoom_Name")&"_Value") pChat_Number=application(session("sRoom_Name")&"_Number") '由于使用了session("sRoom_Name")来保存当前的房间名,因此可以被每个房间的 提交子过程调用。 if pChat_Number>=50 then pChat_Number=0 end if redim tmpChat_Value(pChat_Number) for i=0 to pChat_Number tmpChat_Value(i)=pChat_Value(i) next pChat_Value(0)=Message for i=0 to pChat_Number pChat_Value(i+1)=tmpChat_Value(i) next pChat_Number=pChat_Number+1 application(session("sRoom_Name")&"_Value")=pChat_Value application(session("sRoom_Name")&"_Number")=pChat_Number end sub %>
logname.inc <% sub logname(Chater_Name,Room_Name) pChater_Value=application(session("sRoom_Name")&"er_Value") pChater_Number=application(session("sRoom_Name")&"er_Number") if pChater_Number>=150 then pChater_Number=0 end if pChater_Value(pChater_Number)=Chater_Name&"["&Room_Name&"]" pChater_Number=pChater_Number+1 application(session("sRoom_Name")&"er_Value")=pChater_Value application(session("sRoom_Name")&"er_Number")=pChater_Number end sub %> roomferesh.inc <% sub roomrefresh() pRoom_Number=application("gRoom_Number") pRoom_Value=application("gRoom_Value") for i=0 to pRoom_Number-1 Room_Name=left(pRoom_Value(i),instr(pRoom_Value(i),"(")-1) pRoom_Value(i)=left(pRoom_Value(i),instr(pRoom_Value(i),"[")-1)&"["&application(Room_Name&"er_Number")&"]"&"人" next application("gRoom_Number")=pRoom_Number application("gRoom_Value")=pRoom_Value end sub %> 这个子程序大家可能不太明白,他是用于显示各个房间信息的。 好了,下面是关键的显示页面chatpage.html,它分为三帧,左右为80%,20%,然后 再将右面的分为上下80%,20%,左上的部分就是主的显示页面 showpage.asp <!-- #include virtual="/chat/inc/Convert.inc" --> <%'将内容记入application数组并调整顺序%> <!-- #include virtual="/chat/inc/ChangeRoom.inc" --> <%'处理换房的过程%> <!-- #include virtual="/chat/inc/Logname.inc" --> <!-- #include virtual="/chat/inc/uLogname.inc" --> <% if request.servervariables("Request_Method")="POST" then if request.form("cmdChangeRoom")="换房" then application.lock call changeroom() application.unlock else chat=request.form("txtChat") who=session("sChater_Name") towho=request.form("selShow") common_or_private="common" chat=who&towho&"说:"&chat if request.form("chkPrivate")="private" then common_or_private="private" chat=chat&"[私聊]" end if application.lock call convert(common_or_private&","&who&","&towho&","&chat&"<br>") application.unlock end if end if %> <html> <head> <title> ShowPage </title> </head> <body> <center> <% response.write session("sRoom_Name") %> </center> <% for i=0 to application(session("sRoom_Name")&"_Number")-1 position_one=instr(application(session("sRoom_Name")&"_Value")(i),",") position_two=instr(position_one+1,application(session("sRoom_Name")&"_Value")(i),",") position_three=instr(position_two+1,application(session("sRoom_Name")&"_Value")(i),",") if mid(application(session("sRoom_Name")&"_Value")(i),1,position_one-1)="common" or session("sChater_Name")=mid(application(session("sRoom_Name")&"_Value")(i),position_one+1,position_two-position_one-1) or session("sChater_Name")=mid(application(session("sRoom_Name")&"_Value")(i),position_two+1,position_three-position_two-1) then response.write mid(application(session("sRoom_Name")&"_Value")(i),position_three+1) end if next '大家注意了,这里是关键的关键,房间的显示与悄悄话的处理都在这里了 代码是太长了,但若使用好的数据结构应该能大大简化代码,我会用sever端的 javascript来试一试,希望可以 %> <% end if %> </body> </html> 好长哦,^O^,没办法,高级的功能要付出大的代价,我这里是想告诉大家一种思想 以前你肯定定义过application("var1")这样的变量,但象application(session("sRoom_Name")&"_Value") 这样的呢???我会在近期将它做成一个activex控件,那时大家就方便了。 想要的话给我写个mail就行了。。(出处:热点网络)
|