文章摘要
这篇文章描述了一段VBScript代码,用于在一个Web应用中实现聊天功能的核心逻辑。代码的主要功能包括:
1. **缓存功能**:通过`Response.Buffer=true`设置输出缓存,方便显示不同页面的内容。
2. **错误处理**:使用`On error resume next`忽略程序运行时的错误。
3. **客户端请求判断**:通过`Request.ServerVariables("Request_Method")`判断客户端请求方式,并分别处理GET和POST请求。
4. **客户登陆界面**:当客户端以GET方式请求时,显示一个带有文本框的登陆界面,用户可通过提交按钮进入聊天界面。
5. **聊天界面**:当客户端以POST方式请求时,显示聊天界面,用户可通过提交框发送消息。
6. **消息显示**:在聊天界面中,系统会显示对方的消息,并显示发送者、时间和内容。如果用户输入为空,系统会显示“沉默是金。”
7. **脚本细节**:代码中使用`trim`去除空格,`Application.lock`和`Response.Write`用于显示消息,`Session`用于保存会话信息。
< %@ Language=VBScript %>
< %
Response.Buffer=true ‘ 设置输出缓存,用于显示不同页面。
On error resume next ‘ 忽略程序出错部分
If Request.ServerVariables(“Request_Method”)=”GET” then
‘ 判断客户是以什么方式请求 WEB 页面
‘————————
‘ 客户登陆界面
‘————————
%>
< form method=”POST” action=”chat.asp”>< p>
< input type=”text” name=”nick” size=”20″ value=”nick” style=”background-color: rgb(192,192,192)”>< br>
< input type=”submit” value=” 进入聊天室 ” name=”B1″ style=”color: rgb(255,255,0); font-size: 9pt; background-color: rgb(0,128,128)”>
< p>< input type=”hidden” name=”log” size=”20″ value=”1″>< br>< /p>
< /form>
< %
Response.End ‘ 结束程序的处理
Else
Response.clear ‘ 清空缓存中的内容
dim talk
If Request.Form(“nick”)<>”” then
‘ 判断客户是是否在聊天界面中
Session(“nick”)=Request.Form(“nick”)
End If
‘————————‘
客户聊天界面
‘————————
%>
< form method=”POST” action=”chat.asp” name=form1> < p>< %=Session(“nick”)%> 说话:< input type=”text” name=”talk” size=”50″>< br>
< input type=”submit” value=” 提交 ” name=”B1″>
< input type=”reset” value=” 取消 ” name=”B2″>< /p>
< /form>
< A HREF=”/asptest/shusheng/chat.asp”> 离开 < /a>< br>< br>
< %
If Request.Form(“log”)<>1 then
If trim(Request.Form(“talk”))=”” then
‘ 判断用户是否没有输入任何内容
talk=Session(“nick”)&” 沉默是金。”
Else
talk=trim(Request.Form(“talk”))
‘ 去掉字符后的空格
End If
Application.lock
Application(“show”)=”< table border=’0′ cellpadding=’0′ cellspacing=’0′ width=’85%’ >< tr>< td width=’100%’ bgcolor=’#C0C0C0′>〈/td〉〈/tr〉< tr>< td width=’100%’>< font color=’#0000FF’> 来自 “&Request.ServerVariables(“remote_addr”)&” 的 “&Session(“nick”)&time&” 说:< /font>”&talk&”〈/td〉〈/tr〉< tr>< td width=’100%’ bgcolor=’#C0C0C0′>〈/td〉〈/tr〉< /table>< br>”&Application(“show”)
Application.UnLock
Response.Write Application(“show”)
End If
End If
%>