📄 content.asp
字号:
<%
'dim用来定义普通变量
'定义变量,存储要输出的字符串信息
dim info
'定义change变量,记录用户是否已经加入了新房间
dim changed
'创建数据库连接对象
set newconn=server.CreateObject ("ADODB.Connection")
dbpath=server.mappath("chatroom.mdb")
newconn.open "driver={Microsoft Access Driver (*.mdb)};dbq="&dbpath
'如果用户是通过GET方法提交信息的,即从enter.asp页面中通过点击"进入"链接进入聊天室
'就用Request的QueryString集合取得用户进入的房间的名称和用户的帐号,并赋给变量
if Request.ServerVariables ("REQUEST_METHOD")="GET" then
UserID=Request.QueryString ("user")
myroom=Request.QueryString ("RoomName")
end if
'如果用户是从send.asp页面的表单通过POST方法提交信息的
'就用Request的form集合取得用户新建的房间的名称、用户的帐号和昵称
'房间的名称、用户的帐号和昵称都是通过send.asp页面的隐藏区域标记提交到该页面的
if Request.ServerVariables ("REQUEST_METHOD")="POST" then
myname=Request.Form ("userid")
UserID=myname
myroom=session("myroom")
mynickname=Request.Form ("mynickname")
'更新在线用户表
newsql="update UserOnLine set LastTalkingTime='" & now & "' where OnLineUserID='" & UserID & "'"
newconn.Execute (newsql)
'application的Lock方法用于保证同一时刻只有一个用户在对Application对象操作,这样可以保证数据的一致性和完整性
Application.Lock
'判断send.asp页面的提交的信息是命令还是聊天内容
'如果是命令就判断是执行什么操作的命令,然后执行相应的操作
'如果是用户的聊天内容就直接提交到content.asp页面中
'如果第一个字符是“/”,就表示是命令
'T1是通过send.asp页面的隐藏区域提交到该页面的
if left(Request.Form ("T1"),1)="/" then
'left是函数,它用来取得提交的信息的第一个字符
'如果是"/",就把提交的内容赋给command变量
command=Request.Form ("T1")
'取得提交信息的前2个字符,判断是执行什么操作的命令
select case left(Request.Form ("T1"),2)
'加入新房间
case "/j"
'取得用户要加入的房间的名称
newroom=trim(mid(Request.Form ("T1"),4))
'mid函数表示从T1提交的字符串中返回从第4个开始的其后的所有字符
'因为"/j"后要有一个空格,所以要从第四个字符开始
'trim函数将mid函数返回的字符串的前后空白字符删除后返回
'房间信息表中查找是否有用户请求的房间名称
newsql="select RoomName,IfLocked,WhoCreate from RoomInfo where RoomName='" & newroom & "'"
set newrecord=newconn.Execute (newsql)
newRoomName=newrecord("RoomName")
'定义变量,记录判断房间创建者是否已经离开了他所创建的房间
dim Creator_Leaved
'如果没有该房间,就通知该用户他所请求的房间不存在
if newrecord.bof and newrecord.eof then
application(UserID)="没有这个房间!<br><br>" & application(UserID)
else
'如果用户请求的房间存在,就判断该房间是否已经上锁
if newrecord("IfLocked")=true then
newsql="select OnLineUserID from UserOnLine where OnLineUserID='" & newrecord(3) & "'"
set myrecord=newconn.Execute (newsql)
'如果用户请求的房间已经上锁,就通知用户不能进入
if not myrecord.BOF then
application(UserID)="这个房间已经上锁,不能进入!<br><br>" & application(UserID)
else
'如果房间创建者已经离开了他所创建的房间,就把该房间的锁打开
Creator_Leaved=true'记录房间创建者已经离开了他所创建的房间
newsql="update RoomInfo set IfLocked=false where RoomName='" & newrecord(0) & "'"
newconn.Execute (newsql)
end if
else
'如果用户请求的房间没有上锁或锁已经打开,就通知该房间中的所有用户新成员的到来
if newrecord("IfLocked")=false or Creator_Leaved=true then
application(UserID)=application(newroom)
'从在线用户表中查找在该房间中的所有用户
newsql1="select OnLineUserID from UserOnLine where RoomName='" & newrecord(0) & "'"
'创建记录集对象
set newrecord1=server.CreateObject ("ADODB.Recordset")
newrecord1.Open newsql1,"DSN=ChatRoom",1,3
'通知该房间中的所有用户新成员的到来
do while not newrecord1.eof
tempname=cstr(newrecord1("OnLineUserID"))
if tempname<>UserID then
application(tempname)="<br>" & mynickname & "来了!(请刷新)<font color=blue size=1>(" & time() & ")</font><br><br>" & application(tempname)
end if
'将记录指针移到下一条记录
newrecord1.movenext
loop
'更新房间信息表,新房间聊天成员加1,原来的房间的聊天成员的减1
newsql3="update RoomInfo set HowManyUsers=HowManyUsers+1 where RoomName='" & newRoomName & "'"
newconn.Execute (newsql3)
newsql4="update RoomInfo set HowManyUsers=HowManyUsers-1 where RoomName='" & myroom & "'"
newconn.Execute (newsql4)
'更新在线用户表中该用户所在的房间
newsql2="update UserOnLine set RoomName='" & newRoomName & "' where OnLineUserID='" & UserID & "'"
newconn.Execute (newsql2)
'通知该用户他已进入的他所请求的房间
application(UserID)="你已经进入了房间" & newroom & "<br><br>" & application(UserID)
'定义一个session变量记录新房间名
session("myroom")=newRoomName
changed=true
'关闭记录集对象
newrecord1.Close
set newrecord1=nothing
end if
end if
end if
'查看各个房间的信息
case "/r"
newsql="select * from RoomInfo"
set newrecordset1=newconn.Execute (newsql)
do while not newrecordset1.eof
'将所有的房间信息赋给info变量
info=info & newrecordset1("RoomName") & "--" & newrecordset1("HowManyUsers") & "--" & newrecordset1("Topic") & "--" & newrecordset1("IfLocked") & "--" & newrecordset1("WhoCreate") & "<br>"
newrecordset1.movenext
loop
'将查出的房间信息传递给用户
application(UserID)=info & "<br>" & application(UserID)
'关闭记录集对象
newrecordset1.Close
set newrecordset1=nothing
'查看聊天室成员信息
case "/w"
'如果命令字符串的长度是2,就查看所有在线聊天成员的信息
'len函数返回字符串内字符的数目
if len(trim(Request.Form ("T1")))=2 then
newsql="select onlineuserid,nickname,roomname,logintime from UserOnLine"
set newrecordset2=newconn.Execute (newsql)
'列出当前聊天室中所有在线用户的信息
do while not newrecordset2.eof
info=info & newrecordset2("onlineuserid") & "--" & newrecordset2("nickname") & "--" & newrecordset2("roomname") & "--" & newrecordset2("LoginTime") & "<br>"
newrecordset2.movenext
loop
'将当前聊天室中所有在线用户的信息输出到浏览器页面
application(UserID)=info & "<br>" & application(UserID)
else
'如果这个命令包含的内容多于2个字符,则在命令中必须要有":",即以/w:roomname的形式出现
'left函数取得T1提交的信息从左边开始的前3个字符
'right函数取得经left函数处理后的字符串从右边开始的第一个字符,判断它是否是":"
'如果不是":",就提示命令行输入错误
if right(left(Request.Form ("T1"),3),1)<>":" then
info="命令行输入错误!需要有冒号“:”!<br><br>"
application(UserID)=info & application(UserID)
else
'mid函数取得用户请求的房间名称
temproom=mid(trim(Request.Form ("T1")),4)
'从房间信息表中查找用户请求的房间名称
newsql="select RoomName from RoomInfo where RoomName='" & temproom & "'"
set newrecord3=newconn.Execute (newsql)
'如果用户请求的房间名称存在,就判断该房间中是否有人
if not newrecord3.bof then
newsql="select * from UserOnLine where RoomName='" & newrecord3("RoomName") & "'"
set newrecordset2=newconn.Execute (newsql)
'如果该房间中没有人,就通知用户"该房间暂时没有人!"
if newrecordset2.bof then
application(UserID)="<br>该房间暂时没有人!<br><br>" & application(UserID)
else
'列出该房间中聊天成员的信息
do while not newrecordset2.eof
info=info & newrecordset2("UserId") & "--" & newrecordset2("RoomName") & "--" & newrecordset2("IpAdd") & "--" & newrecordset2("TimeLogin") & "<br>"
newrecordset2.movenext
loop
application(UserID)="<br>" & info & "<br>" & application(UserID)
end if
else'没有这个房间
info="<br>没有<font color=vbred><strong>" & temproom & "</strong></font>这个房间!<br><br>"
application(UserID)=info & application(UserID)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -