Using ASP to build a simple chat room

Source: Internet
Author: User

After a period of ASP learning, the following we combine the content learned to build a simple chat room, although very simple, but we can through him to master a chat room to establish the basic process, and can constantly improve its function.

The following are the main steps:

1, add the code inside the Global.asa file. This section of code deals with the Application_OnStart event, in which a 15-element data is defined, and assigns it to a Application object's properties. The contents of the Global.asa file are as follows.

<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
SUB Application_OnStart
dim maChats(15) 定义一个具有15个元素的数据.
Application("gaChats")=maChats 存储谈话的内容.
Application("giCounter")=0 存储已有的谈话数目.
END SUB
</SCRIPT>

2, add the code to the aspchat.asp file. Note that this part of the code is added between the two horizontal lines in the home page, which is between two

in the text. The program first determines whether the application of the homepage is a "post" method, because in this case the method of submitting the window is "post" In general, the first time you apply for this homepage, the "Get" method is used. So if a "post" method is used, it means that a visitor is submitting his or her conversation to a new application, which is going to be done, because there is another conversation. Otherwise, the first time a visitor applies for this homepage, Didn't submit any conversations, so just have to show the current conversation.

a simple chat room




<%
Process input
if Request.ServerVariables (" Request_method ") =" POST "then
Mark Speaker
If Len (Request (" txtwho ")) >0 then
session (" Sswho ") =request (" txtwho ")
End If
to block the Application object
Application.Lock
To create a local reference pointer
mlcounter=application ("Gicounter")
machats= Application ("Gachats")
If the number of rows written is more than 10, restart the count.
If Mlcoundter>9 then
mlcoundter=0
End If
to increase user input, counter plus 1
Machat (Mlchounter) =session ("sswho") & ":" &request ("txtcents")
Mlcounter=mlcounter+1
Set the local variable to be valid within the scope of the application.
Applicati "Gicounter") =mlcounter
Application ("Gachats") =machat
Eliminate blocking of application objects
Application.UnLock
End If
%>
<%
Write to textarea
if application ("Gicounter") =0 then
lstemp= Application ("Gachats") (0)
Else
for x=0 to Application ("Gicounter")-1
lstemp=lstemp& "
" Application ("Gachats") (x)
Next
End If
response.white lstemp
%>

3, and finally the content of the txtwho initialization. That is, when the viewer enters his or her own name once, the user does not have to enter it again, in order to distinguish each of the different browsers, where the "session" object is used. Using session ("sswho") Record the name of the visitor. The following code is written directly below the above code into the aspchat.asp.

<form method="POST" action="aspchat.asp" name="frmAsp">
<div align="center"><center><p>发言:<input type="text" name="txtCents" size="34"></p>
</center></div><div align="center"><center><p>对象:<input type="submit" value="送出" name="B1"></p>
</center></div>
</form>

4. Complete. Just put the 2, 3 step code before and after adding the complete HTM file code and then save as aspchat.asp on it, this is a very simple ASP chat room program, we can continue to strengthen its function as the study of the depth!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.