Building Home chat room with ASP

Source: Internet
Author: User
Tags exit count end functions session id variable trim access
Chat Room | Chat rooms have a chat room on the home page that adds a lot of spice to your home page, especially in an Intranet or LAN. However, traditional CGI programming is a headache, with the popularity of ASP technology, the production of home chat room is no longer a terrible thing. Come with me step by step, have your own chat room space.
First, the principle of the home chat room
1, home chat room is not mysterious, the name of the communication between the members of the way is not as the performance of the direct connection, but through the Web server as an intermediary. At this time, the Web server is more like a big mailbox, which contains a lot of letters, there are people can see the public letter, there are private letters (both whispering), feel like the real one is in direct communication with each other.
The rationale for ASP is no longer described. The idea of constructing the chat room is the application of its internal object application and session. The Application object corresponds to the concept of a global domain variable, and deletes are created when the server is started off. Any customer who changes it at any time will cause a change, so the Application object is used in the chat room program to hold the public information. The session object corresponds to the local domain concept of a customer, created when the customer enters the site page, and each customer has a unique session ID (this ID is saved in the client cookie so the browser is allowed to accept cookies). The change of Session variable is independent to each customer, and it can save the same station point of the same customer different pages of data, so in the chat room program with the session object to save the chat members of special information, such as chat code, whispering and so on.
Using ASP to build chat rooms fully embodies the differences between ASP and CGI programs. CGI program, a process is created for each client connection, and its cost to the resource is great. and ASP Application object to save their own data, sharing a process space, so the efficiency of ASP is very high.
Ii. construction of the basic framework
A total of two pages, one to use as a login interface, a main chat page. Login Page Login.asp is very simple, is a from form, submitted after the chat code and other information to the session variable session ("name"), and then the navigation (Response. Redirect mainchat.asp) to the main page mainchat.asp. The main chat page consists of two frame:
Frame1 the words.asp page to display the chat content. Frame2 chat.asp page to send chat information.
2, set application and Session object
It is natural to use Application ("show") to store public information and use the session ("name") to store the chat room code. What do you store with your private whisper? Here is a very ingenious way: Use Application ("name") to store the message! Each chat code corresponding to a application variable, in the display page with 〈% = Application ("name")%> will be displayed under this code of whispers.
3, Display page words.asp key statement:
<HTML>
<meta htpp-equiv= "Refresh" content= "ten" >//Refresh page every 10 seconds
...
<%=application (Session ("name"))%>//displays a whisper, if any
...
<%=application ("show")%>//Display Public dialog information
...

</HTML>
4, chat.asp the main implementation of the message sent, including a form to fill in to say and whisper the recipient (for the empty representation of public information), submitted to the chat.asp itself, the form data to their respective variables: To send the message to the application ("Show "), the receiver of the Whisper is sent to Applicatio (Secret). Key statements for chat.asp:
...
Talk=trim (Request.from ("Txbox")//For message text input box,//This statement extracts the message content to send.
Secret=trim (Request.Form ("Secret")//secre for the Whisper recipient text input box//This statement extracts the whisper recipient.
If secret= "" Then
If talk< > "" then//Send public information to application ("show")
Application.Lock
Application ("Show") =session ("name") & ":" &Talk& "<br>" &application ("show")
Application.UnLock
End If
Else
If talk< > "" then//send out a private message.
Application.Lock
Application (Secret ") =session (" name ") &" Whisper to You: "& Talk//Here Secret is the code name for the receiver of the Whisper, control Application.UnLock// Words.asp, you will find that only the receiver will show this whisper, very clever bar.
End If
End If
...

In this way, the basic framework of the chat room has been built, you can find a few friends to say. But also very simple, next will give the chat room to add some important functions such as online statistics, chat code list, chat action design, refresh Time control.
Third, the chat room function expands the acceptance
1. Online population statistics
We set up a global application ("PEOPENUMW") variable to hold the number of online people and use a session ("access") variable to indicate whether the customer status is already in the chat room. Each time you log into the chat room, according to the state of the ac-cess Peoplenum to do one or minus one operation, so as to avoid the customer repeat login and repeat the count (such as the customer repeatedly click the Back button or the Forward button). The specific implementation is:
When you enter the chat room to judge the access=0 (not into the chat room), then you can enter the main chat room after the Login.asp page is submitted, and the Peoplenum plus one, access logo set one; When Access=1 (has entered the chat room did not exit correctly), in the entry login. Pop-up alert box when ASP page "You are not out of the chat room, please do not log in again." "and navigate to the chat room to allow the customer to exit." The peoplenum does not increase at this time.
Exit Chat The situation is similar when Access=1, when the peoplenum minus one, when access=0, Peoplenum unchanged.

Perhaps a friend would ask a question, if the user quits normally, such as directly switch the address bar or close the browser, can the correct statistics of this number of people? OK, each session has a On-sessionend event that triggers the runtime at the end. You can therefore include in this event handler:
If session ("Access") =1 Then
Application.Lock
Application ("Peoplenum") =application ("Peoplenum")-1
Application.UnLock
End If

In this way, when a customer exits abnormally for a period of time (the timeout attribute of the session is determined), the number is automatically subtracted.
2, chat Room code control
Chat room code as a chat member of the logo, requires the ability to list the line Code table, and does not allow the same name. My solution to this problem is to use a built-in object in VBScript dictionary, which is a key-valued array, you can add or remove members through the method, and can also use function exists to determine whether a key value exists, which is just used to prevent the same name.
First we want to create a Dictionary object that can be created in a script using set Nickname=createobject ("Scripting.Dictionary"), which we can also pass in global.asp (object) Tag is created, you can reference it anywhere.
(OBJECT runat= "SERVER" scope=application id=ohatname proid= "Scripting.Dictionary") (/object)
Note: global.asp is an ASP-specific file placed in the root of the Web site, which executes global.asp files whenever the Web server is started or shut down, so it is often used for initialization or cleanup.
Each time you log on, you first pass the code to session ("name"), and then
Nickname=trim (Requeat.form ("Txtbox"))
...
Chatname.add Nickname,nickname

In this way, the code for all online customers is saved in the Chatname.
And on each exit, delete the chat code: Chat-name.remove session ("name")
How to list the online customer code in the chat room? You can change the private message text input box to a Drop-down list box, click it to pull all the code list, very simple and convenient.
(select Name= "Secrt ')
(option Selected)//The default choice is null, that is, not to send private whispers.
(% for=1 to chatname.count-1%)
(option) (%=chatname. Session ("name")%)//list all code names.
(%next%)
(/select)
3, chat room action Design
If you often go to BBS chat room, it must feel that the action is very interesting. Do you want to add an action to your home chat room? It's too easy as long as you want it. In the author's chat room using the type of BBS action mode, (in fact, the model can be arbitrary, by their own decision.) For example, I enter "//kick menu" will show "xxx to play menu gotten me", interesting. It is very simple to determine whether the first two characters of the input character are "//" and then select the displayed content based on the words that follow.
If Instr (TALK, "//") =1 Then
Select Case Rtrim (Mid (talk,3,5))//5 characters after "//"
Case "Kik"
Application.Lock
Application ("Show") =session ("name") & "The Right (Talk,len (Talk) -7) &" kicked gotten me. "& Application (" Show ")
Application.UnLock
Case ...
End Select
End If

A few string functions of VBScript are used here to extract the action information from the talk. Chat room can arbitrarily expand the action, in the author's chat room there are many "Dahua West Tour" of the lines.
4, other function expansion
* Set Refresh Time
In words.asp (META http-equiv= "Refresh" content= "10") set a fixed refresh time of 10 seconds, but in practice, users need to set the refresh time according to the circumstances, such as faster than expected to refresh time is short, You want the refresh time to be longer when you are slow or when you want to view past information. We can add a text input box to the chat.asp to enter the time that the customer wants in the refresh time to pass to the session ("Freshtime") and change the word.asp statement to: (% Response. Write "(META http-equiv=" "Refresh" "Content=" "&session (" Freshtime ") &" ")"% ".
* Customers independently display Help lists and control commands
As you may have noticed, the aforementioned



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.