Re:(90分赠送)关于ASP或着JAVASCRIPT的无刷新技术如何实现,再者还有一问题,见详文`

nibani 2002-05-30 12:10:41
上篇不能再贴了。
~~~~~~~~~~~~~~~~~~~~~~
在save.asp页面中,我们对聊天的信息做了很多处理。其基本的思想很简单,就是在聊天信息中加入不同的特征字符,来表征不同的功能。值得注意的是,对于有些只在客户端交互的内容,我们并没有将其存入到服务器的application对象变量中。比如用//?查询时,我们直接用parent.show.document.write写出来,这样可以减轻服务器的负担。对于踢人功能,我们在此就是简单的清除application("userliststr")中的姓名信息。当用userlist.asp页面来刷新时,将不再有该姓名信息出现,从而达到踢人的目的。锁房和开锁功能是和main.asp页面来配合工作的。为了锁住房间,在本页面中,我们只需要在application("lockroom")变量中加入本房间的名称。在main.asp页面中,我们查看application("lockroom")变量。如果其中有我们想进入的房间名称,则表示该房间已经被锁定,聊天室程序将禁止用户进入。否则,用户可以进入该房间。
信息存储完成后,我们要用read.asp来显示信息。该页面的源程序如下。
<!--#include file="funlib.asp"-->
<%
nowid=trim(Request.QueryString("id"))'获取本机聊天信息存储指针
chatroom=trim(Request.QueryString("chatroom"))
nickname=trim(Request.QueryString("nickname"))
'从服务器中读出本机还未读出的信息并显示在聊天窗口中
for i=1 to (clng(application("id")) - clng(nowid)) mod 100
msgstr=application(i+clng(nowid))
if chatroom = getroomname(msgstr) then
if getprivatename(msgstr)="" then
if left(msgstr,2)="系统" then
msgstr=getsaywords(msgstr)
else
msgstr="<a href=javascript:parent.input.towho('"&getnickname(msgstr)& "');>" & getnickname(msgstr)&"</a>" & getemotion(msgstr) &"<a href=javascript:parent.input.towho('" & gettowhoname(msgstr) & "');>" & gettowhoname(msgstr) & "</a>" & getsaywords(msgstr)
end if
%>
<script language="JavaScript">
parent.show.document.vlinkColor="#8888ff"
parent.show.document.linkColor="#8888ff"
parent.show.document.write("<%=msgstr%>");
parent.show.document.write("<br>");
</script>
<%
elseif getprivatename(msgstr)= nickname or getnickname(msgstr)=nickname then
if left(msgstr,2)="系统" then
msgstr=getsaywords(msgstr)
else
msgstr="<font color='#00DF00'>(悄悄地)</font>" & "<a href=javascript:parent.input.towho('"&getnickname(msgstr)& "');>" & getnickname(msgstr)&"</a>" & getemotion(msgstr) & "<a href=javascript:parent.input.towho('" & gettowhoname(msgstr) &"');>"& gettowhoname(msgstr)&"</a>" & getsaywords(msgstr)
end if
%>
<script language="JavaScript">
parent.show.document.vlinkColor="#8888ff"
parent.show.document.linkColor="#8888ff"
parent.show.document.write("<%=msgstr%>");
parent.show.document.write("<br>");
</script>
<% end if
end if
next
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<meta http-equiv=refresh content='2;url=read.asp?id=<%=application("id")%>&nickname=<%=nickname%>&chatroom=<%=chatroom%>'>
<base target="autofresh">
<script language="JavaScript">
</script>
</head>
<body>
<script language="JavaScript">
parent.show.window.scrollTo( 0, 10000);//使页面自动滑动
</script>
</body>
</html>
在本页面中,我们首先从服务器中读取还未在本机上显示的聊天信息。本机已经读出的聊天信息指针我们通过nowid=trim(Request.QueryString("id"))语句从上个页面获得。而聊天室中总共的聊天信息指针我们从application("id")获得。接下来,我们从读取的信息中获取聊天信息,然后用parent.show.document.write("<%=msgstr%>")语句在聊天窗口中显示出来。注意本页面中将asp脚本和javascript脚本共同使用的方法,这是编写聊天室程序的技巧。然后,我们让本页面每2秒刷新一次,并传递所需要的各个信息。最后,我们让聊天窗口自动滚动。scrollTo()函数的第一个参数是指页面横向滚动的范围,第二个参数指页面纵向滚动的范围。用来显示聊天内容的showwords.asp页面很简单,只是一个空白页面。
最后,我们介绍一下userlists.asp页面。该页面有两个主要功能。一个是显示在线用户名称;另一个功能是判断用户是否在线。源程序如下。
<!--#include file="funlib.asp"-->
<%Response.Expires=0
chatroom=Request.QueryString("chatroom")
nickname=Request.QueryString("nickname")
isintheroom=false
' 查看是否为房间的主人
hoststr=application("hostname")
if getroomname(hoststr) = chatroom then
if getnickname(hoststr) = nickname then
application.lock()
application("hostname")="$$@@,"
application.lock()
session("host")="host" '获得房主权限
end if
end if
'初始时姓名列表的处理
If Application("UserListStr")="" Then
Application.Lock()
Application("UserListStr")="$大家$@@##,"
Application.UnLock()
end if
'显示在线人数
if Application(chatroom) = "" then
usernum = 0
else
usernum = Application(chatroom)
end if
Response.Write usernum &"人在线"&"<br>"&"<a href=javascript:parent.input.towho('大家')>"&"大家</a> <br>"
'更新用户所在时间,并显示用户名单
UserListStr= Application("UserListStr")
UserListStr=Left(UserListStr,len(UserListStr)-1)
User=Split(UserListStr,",")
for i = 1 to ubound(User)
UserName=getnickname(User(i))'待查看用户名称
userroom=getroomname(user(i))'待查看用户所在房间
nicktime=getfreshtime(user(i))'本用户名称
If userroom=chatroom Then
population=population+1'计算房间人数
if username=nickname then
user(i)=replace(user(i),"#" & nicktime & "#","#" & time() & "#")'更新本用户时间
nicktime=getfreshtime(user(i))'待查看用户的更新时间
isintheroom=true
end if
'判断查看的用户是否在线
if abs(datediff("s",nicktime,time()))>20 then
user(i)=""
Application.Lock()
application("id")=application("id") + 1
application(application("id"))="系统$$@" & chatroom &"@##&&<font color='#008040'>呜呜呜呜呜:" & username & "走了!</font>"
Application.UnLock()
else
Response.Write "<a href=javascript:parent.input.towho('" & username &"')>"& username & "</a> <br>"
end if

end if
next
userliststr=""
'重新构造application("userliststr")变量
for i=0 to ubound(user)
if user(i)<>"" then
userliststr=userliststr & user(i) & ","
end if
set user(i)=nothing
next
Application.Lock()
application("userliststr")=userliststr
Application(chatroom)=population'赋值房间人数
Application.UnLock()
set userliststr=nothing
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
body {font-size:9pt;}
-->
</style>
<title></title>
<meta http-equiv=refresh content='6;url=userlists.asp?nickname=<%=nickname%>&chatroom=<%=chatroom%>'>
<base target="_self">
</head>
<body bgcolor="#ccccff">
<%
if isintheroom=false then'如果本人以不在房间内关闭窗口
%>
<script language="JavaScript">
parent.close();
</script>
<%end if%>
</body>
</html>
可以看到,本页面每6秒刷新一次。每次需要更新自己的时间,同时查看本房间内其他用户更新的时间。如果其时间和自己此次更新的时间相隔超过20秒,则认为该用户已经离开房间,并做出相应的处理。如果发现本人已经不在该房间内,就自动关闭窗口。这主要是配合踢人功能起作用的。本页面中用到了datediff()函数,该函数用来计算两个时间之差。函数的第一个参数指明是以何种时间单位计算,后两个参数指明要计算的两个时间。abs()函数取得参数的绝对值。本程序中datediff()之值可正可负,故用abs()函数取得绝对值后再加以判断。到此为止,主要的页面我都已经介绍了。至于功能区,我们暂时仅在其中加入了一个帮助页面的链接,不再赘述。
三 功能展望
虽然以上的聊天室程序比较简单,但其具有一个好的聊天室的基本结构。在此基础上我们可以非常方便的添加很多功能。最容易实现的就是开房功能。想到怎么做了吗?对,就是找个没有用过的房间名称,将其赋值给chatroom变量,然后调用main.asp页面就可以了。因为我们的聊天室程序除了登录页面有固定的房间信息,其他的页面都是动态获得房间信息的。因此,实现开房功能的易如反掌!看到一个好的基本结构的优点了吧。既然能够实现开房功能,那么实现二人世界的功能也就很容易了。只需要请求方发出一个请求,比如说“///ask 昵称”,然后应答方回应一个应答,比如“///agr 昵称”。此时,应答方用一个特殊的名字(比如说两个人的昵称)作为chatroom变量的值调用main.asp页面。请求方在接收到回应信息后用相同的名字调用main.asp页面就可以了。当然,如果我们在页面中添加数据库支持,就可以实现很多个性化的聊天功能。如自定义的聊天语句,积分等级功能等等。最后,可别忘了给聊天室加上图像功能哟!


...全文
43 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
nibani 2002-05-30
  • 打赏
  • 举报
回复
但愿你有用。
真的佩服 BrightEye(问个不休),在我贴的过程中也能打断。呵呵。
alarm_zq 2002-05-30
  • 打赏
  • 举报
回复
呵呵,我也很佩服你啊,写了这么多。祝愿你了,朋友!

28,408

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧