Chat.asp Chat Program Writing method _ Application Skills

Source: Internet
Author: User
Tags http post servervariables trim

Maybe friends have seen the way ASP chats are written in some magazines, but the author has written a simpler program here, using only an. ASP file. Please clip the following code into a memo book and save it as a chat.asp.

<%@ language=vbscript%> <% response.buffer=true ' Set output cache for displaying different pages. On Error Resume Next ' Ignores program errors section If request.servervariables ("request_method") = "get" Then ' determines how the customer requests a WEB page '-------- ----------------' Customer Login interface '------------------------%> < form method= "POST" action= "chat.asp" >< p> < Input type= "text" name= "Nick" size= "value=" Nick "style=" Background-color:rgb (192,192,192) ">< br> < Input type= "Submit" "value=" into the chat room "name=" B1 "style=" Color:rgb (255,255,0); font-size:9pt; Background-color:rgb (0,128,128) "> < p>< input type=" hidden "name=" Log "size=" value= "1" >< br>& Lt /p> </form> <% Response.End ' End process Else response.clear ' empty cache contents Dim talk If Request.Form ("Nick") <> "Then" determines whether the customer is in the Chat interface session ("Nick") =request.form ("Nick") End If '------------------------' Customer chat interface '------ ------------------%> < form method= "POST" action= "chat.asp" Name=form1> < p><  %=session ("Nick")%> speak:< input type= "text" name= "talk" Size= ">< br> < input type=" submit "value=" submitted "Name=" B1 "> < input type=" reset "value=" Cancel "name=" B2 "></p> </form> < A href="/asptest/shu Sheng/chat.asp "> Departure </a>< br>< Br> <% if Request.Form (" log ") <>1 then If trim (request.fo RM ("talk") = "then" Determines whether the user has not entered any content talk=session ("Nick") & "Silence is Golden." "Else Talk=trim" (Request.Form ("Talk")) ' Remove the space end If Application.Lock application of the character ("show") = "< table border= ' 0 ' C ellpadding= ' 0 ' cellspacing= ' 0 ' width= ' 85% ' >< tr>< td width= ' 100% ' bgcolor= ' #C0C0C0 ' >〈/td〉〈/tr〉< tr >< TD width= ' 100% ' >< font color= ' #0000FF ' > From ' &request.servervariables ("REMOTE_ADDR") & " &session ("Nick") &time& "Say:</font>" &talk& "〈/td〉〈/tr〉< tr>< td width= ' 100% ' Bgcolor= ' #C0C0C0 ' >〈/td〉〈/tr〉</table>< br> ' &application ("show") application.uNlock Response.Write Application ("show") End If%> 

  

Now let's take a step-by-step analysis of this chat room program.
First of all, because all the customers in the chat room have to be able to share information, it is unavoidable to use object application with application-level variables, which is the key to establishing a Chat program where all the conversation data is stored in an application-level variable so that all customers can read it. We can use the Request object we have learned to get the conversation entered by the customer, save it in the variable talk, and then deposit the talk value in the application-level variable show, as follows:

<% application ("show") =talk&application ("show")%>

The next thing to consider is what to do when different customers are working on the Chat application at the same time. This problem is actually written to the same record as the two users in the database, if two users write to the same application-level variable at the same time, the modifications made by one user are overwritten by the actions of another user, so if the concurrent access to the Appliation object data does not take some measure Will cause two of users to attempt to submit to an ASP chat room application at the same time, a user's submission will be overwritten by another user's submission, disappearing before anyone in the session is seen. To avoid such problems, we need to use the Lock property of the Application object to constrain properties that allow only the current user to edit or increase the Application object, so that when the user starts modifying the application-level variable, the Application object is explicitly Unlocked, the properties of the Application object can only be edited by that user, and if there are other users requesting to edit the Application object at this time, those users will wait in line to know that the application is Unlock. As follows:
application.lock
Application ("show") =talk&application ("show")
Application.UnLock
Now that you know the core of the entire program, consider how to save the customer information, and here we'll use a session-level variable that will keep the customer's nick in sessions. For example,

Session ("Nick") =request.form ("Nick")
Finally, we want to consider how to handle various events in only one. asp file, such as Customer login interface, customer chat interface. Due to the first request from the customer. asp files, the simple HTTP GET method is used, but when the customer adds data to the form field on the page and submits the form to itself, the. asp file is requested again, but this time the data is delivered via HTTP POST. The way a file is requested can be determined in two ways. One is to test whether the Request.Form collection contains members, and if not, indicates that no data has been sent to the form's processing. The second is to use Request.ServerVariables ("Request_method") variables. If a form is requested through an HTTP get method, the variable returns "get" and returns "POST" if the form commits processing. Since the latter method is more straightforward in determining how the file is requested, we use the following code to determine:
if Request.ServerVariables ("request_method") = "Get" Then
because we set up the ASP cache, so when the program to judge the way the page request is get, the program runs Response.End, to end all the following operations, otherwise run response.clear, empty the contents of the cache, and continue to run the program. So that we can use the same. asp file to show customers different interfaces according to different situations.
Okay, I have this ASP Chat the core part of the program to tell you, the rest of the details of the problem please read your own program it, in fact, this program still has a lot of deficiencies, the biggest problem is that I did not write automatic refresh function, so if you do not speak in the Chat, That would not see what other customers were saying.

Chat in the Internet charm everyone is aware of, in fact, with the ASP also fully can do with IRC similar powerful functions, of course, this need to write more code, if you are interested in words can be more in-depth study, and you exchange experience results.

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.