Example analysis: Build your own chat room with ASP

Source: Internet
Author: User
Tags define array object variables variable client access
Chat room Chat Room You must have been there, but would you like to set up your own chat room? In fact, this is not difficult, the Active server script provides the Application object and the Session object, and 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 the page, which allows all users who access it to share information, and can persist data while the Web server is running, for a Application object. The session object can also maintain data over time during a user's visit, making it easy to build your own chat application with these two objects.
----One, application objects:----1. Property: The Application object has no built-in properties, but the user can define its own property:----Application ("property name") = value, and once the property is assigned, it will persist until the Web server shuts down the service and it can be read by all users, So you can use it to send conversations between users. ----2. Methods: When two users write to the value of the application attribute at the same time, the modification of one side will be directly overwritten by the operation of the other party, in order to avoid this phenomenon, the user can call the lock method to lock, This allows only the current user to be able to operate on application properties, and the user can then invoke the Unlock method to unlock the operation, allowing other users to modify the application properties. ----3. Events: Creating an Active Server application requires the creation of a Global.asa file in the virtual home directory of the application on the Web server, which contains event-handling routines for application objects and session objects, usually application_ The OnStart event is used to define attributes at the application level. ----Two, create an chat application: The program runs as shown in the following figure (slightly)----1. To set the application's variables: Here you need to create two application-level variables, gchars array to hold the user's conversation, Gcounter used as a counter, control the number of pages displayed, where we let the page display the most recent 10 lines of conversation content. These variables must be initialized when the application starts, so they are created in the Application_OnStart event in the Global.asa file: < script language= "VBScript" runat= "Server" >
Sub Application_OnStart ()
Dim Lchars (10)
Application ("Gchars") =lchars
Application ("Gcounter") =0
End Sub
</SCRIPT >----2. Determine how ASP is handled: When the user first requests the ASP file, using the Get method, and then, when the user entered the conversation after the submission of the Post method, where the form is submitted to itself, so this ASP file will be requested again, we passed the test request. Servervariales ("Request_method") variable to determine how the file is requested: IF request. Servervariales ("request_method") = "POST" then----3.      Identify the Speaker: When the user submits the conversation for the first time, enter his or her own name, once the data is entered in the Txtname box, the program creates a session-level variable to hold the user's name and automatically displays it in the Txtname box, and the user does not need to enter it again unless you want to join the conversation with another name. IF Len (Request ("Txtname")) >0 Then
Session ("Ssname") =request ("Txtname")
End If
< h5 > Your name:
< input type= "type" name= "Txtname"
Length= "value=<"%=session ("Ssname")% > >--------4. Dealing with a user's conversation: first to determine the number of conversations that have been written for chat, to facilitate reading, to limit the number of rows displayed here to 10 lines, or to 0 if application ("Gcounter") is greater than 9, The speaker's name and content are then stored together in the application ("Gchars") Array: Application ("Gchars")
(Application ("Gcounter")) =session ("Ssname") &
":" & Request (Txttalk) then add the Counter 1:application ("Gcounter") =application ("Gcounter") +1----5. To write the contents of an array to the client's browser: After the user submits the conversation, the program must write the contents of the array to the client's browser so that everyone in the chat room can see the content being submitted: If Application ("Gcounter") =0 Then
Lstemp=application ("Gchars") (0)
Else
For x=0 to Application ("Gcounter")-1
Lstemp=lstemp & "< br >" & Application ("Gchars") (x)
Next
End If----Finally, use the Response.Write method to write the value of the LSTEMP variable to the customer's browser: Response.Write lstemp----The complete code for default.asp below: <% Response.expires=0
response.buffer=true% >
< HTML >< Head >< title >chat sample</title >< body >< Center >
< h3 > My chat room

<% if Request.ServerVariables ("request_method") = "POST" Then
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 & "< br >" & Application ("Gchars") (x)
Next
End If
Response.Write Lstemp% >
< hr >< Center >
< form action= "Default.asp" Method=post name= "Aspform" >
< b >< a href= "Default.asp" >
Update Show </a ></b >
< h5 > Speaking:
< input type= "text" name= "Txttalk" size= "a" >< br >
< h5 > Your name:
< input type= "text" name= "txtname" length= "20"
value=<%=session ("ssname")% > >
< input type= "submit" Name= "Cmdpost" default= "true" value= "send" >
</form ></center ></body >
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.