Chat room wants you must have to go, but don't want to build your own chat room? In fact, this is not difficult. Active Server Script provides an Application object and a session object. The Application object represents an Active Server application, which is a web page. The session object represents a user, representing a user's access to this page. Through Application objects, all user sharing information can be accessed, and the data can be done while the web server is running, and the session object can also maintain data from the user's access, and use these two objects. It is very convenient to build your own CHAT app.
---- One, Application object:
---- 1. Property: Application object does not have built-in properties, but users can define their properties:
---- Application ("Property Name") = value, once the property is assigned, it will always exist until the web server closes the service, and it can be read by all users, so you can use it to send a conversation between users. content.
---- 2. Method: When two users simultaneously writes the value of the Application attribute, one party modification is directly covered by the other operation, in order to avoid this, the user can call the Lock method to lock, so only The current user can operate the properties of the Application, and the UNLock method unlocks after the user completes the operation, so that other users can also modify the properties of the Application.
---- 3. Event: Creating an Active Server application requires the Global.asa file to create a Global.asa file on the web server, which contains an event handler for Application objects and session objects, usually, Application_onstart events are used to define properties of the application level. .
---- II. Create a CHAT application: The program is running as shown in the following figure (omitted)
---- 1. Set the variable of the application: You need to create two application-level variables, gchars array is used to store the user's conversation content, GCUNTER uses the counter, the control page is displayed, here we let the page display the most recent 10 lines Conversation. These variables must be initialized when the application starts, so they are created in the Application_onstart event in the global.asa file:
SUB Application_onstart ()
Dim LChars (10)
Application ("gchars") = lchars
Application ("gcounter" = 0
End Sub
script>
---- 2. Determining the way the ASP is processed: When the user first requests this ASP file, use the get method, then, when the user is submitted, it is submitted when the content is submitted, the post method is submitted, where the form is submitted to itself, so This ASP file will be requested again, we determine the way the file is requested by testing the request. ServerVariales ("Request_Method") = "post". ---- 3. Determine the speaker: When the user needs to enter his name for the first time, after entering the data in the TXTName box, the program will establish a session-level variable to store the user name, and automatically display it in the TXTNAME box, the user No need to enter again, unless you want to join the talks with another name.
IF LEN (Request ("TXTName")> 0 THEN
Session ("SSName") = Request ("txtname")
END IF
Length = "20" value = <% = session ("ssname")%>>>>>
----
---- 4. Handling the content of the user: First, determine the number of rows that have been written for CHAT, for easy reading, here will display the number of lines to 10 lines, if Application ("gcounter" is greater than 9, then set it to 0 Then store the talkient name and content to the Application ("gchars") array:
Application ("gchars")
(Application ("gcounter") = session ("ssname") &
":" & Request (txttalk)
Then add the counter 1: Application ("gcounter") = Application ("gcounter") 1
---- 5. Write the contents of the array to the customer's browser: After the user submits the contents, the program must write the array content to the customer's browser, so that everyone in the chat room can see the submitted conversation:
IF Application ("gcounter") = 0 THEN
LSTEMP = Application ("gchars") (0)
Else
For x = 0 to Application ("gcounter") - 1
LSTEMP = LSTEMP & "
" & Application ("gchars") (x)
NEXT
END IF
---- Finally, write the value of the LSTEMP variable to the customer's browser with the response.write method:
Response.write lstemp
---- The full code of Default.asp is given below:
<% response.expires = 0
Response.buffer = True%>
IF LEN (Request ("TXTName")> 0 THEN
Session ("SSName") = Request ("txtname")
END IF
Application.lock
McOUNTER = Application ("gcounter")
Mchars = Application ("gchars")
IF McOUNTER> 9 THEN
McOUNTER = 0
END IF
Mchars (McOUNTER) = session ("ssname")
& ":" & Request ("txttalk")
McOUNTER = McOUNTER 1
Application ("gcounter") = mcounter
Application ("gchars") = mchars
Application.unlock
END IF%>
<% IF Application ("gcounter") = 0 THEN
LSTEMP = Application ("gchars") (0)
Else
For x = 0 to Application ("gcounter") - 1
LSTEMP = LSTEMP & "
" & Application ("gchars") (x)
NEXT
END IF
Response.write lstemp%>