tạo ứng dụng webchat phần 1

tạo ứng dụng webchat phần 1

Thể loại: Hệ điều hành
Lượt xem: 13,115Lượt tải: 7Số trang: 13

Mô tả tài liệu

ứng dụng tạo web chat cho forum, blog ngay nay, phong cách rất dơn giản dễ hiễu, không quá phức tạp

Tóm tắt nội dung

683 C HA PT E R 2 is a useful CGI program that allows a number of people on the World Wide Web to talk to one another It differs from a BBS (bulletin board system), in which the messages are typically or days after they are posted. The ability to chat on the Web can be a quick way to hold a “virtual meeting.” Figure 26.1 shows an example of what WebChat looks both WebChat and WebBBS store messages for other peo- ple to read, there is a major in how the user sees and The BBS long-term storage of messages, data such as the date and time the message is posted. The BBS also users to branch out into different topics in “threads” the other hand, WebChat the quick posting of much like a among a group of people. Dialogue to flow swiftly in small, easily digested chunks. the topic is being discussed by everyone at the same time, there is little room for different people many different things in the same chat session. Thus, there is no reason to keep track of of 26.1 An example dialogue in people are things in real feature of WebChat is the ability to refresh or display new as quickly as desired. This is done using the META HTML tag to force refreshes within a certain time includes many features designed to this kind In WebChat, users can refresh messages using a button that 26: in plain view. In addition, if the user is using a browser such that supports the META REFRESH HTML tag, the user can choose to have the chat messages refresh or redisplay at are displayed in order of posting from to oldest so that users can quickly look through a list of In addition, users can specify whether to see only new time they refresh the screen or to include a number messages. Viewing several of the previous posts along with new ones tends to provide the user with greater default, messages are posted to everyone, and the user’s is embedded as part of a posted message. This quick posting. By default, posted messages are seen by the user has a choice of entering a different username to speci- fy whom the message should go to; the message is then entered as a pri- vate message from one person to another. This is option analogous a comment to someone else in the middle of a larg- er frames are messages in one frame while the user types messages in another feature allows a user to set a high refresh rate for messages, while leaving the message form intact while the user is typing a message. Figure 26.2 shows an example of also has options such as the of a user’s entry into the chat area, allowing people to keep track of who is currently in the system. Also, when a person or she is to announce the departure by pressing the Log Off button. Nothing is more than to find out the person you were chatting with has left the room! In addition, WebChat can be to remove old messages by age and by number of messages. For example, if WebChat is used for it is generally not useful to keep the for more than an hour. you may want to 26: that not more than 10 or 20 messages stay around at any because messages older than the first 10 may be to course of On the other hand, on other chat may want to keep the messages around for a long time to keep a of the or 26.2 WebChat with frames AND chat files on the CD-ROM will install into a Chat. The files and with this with their required are shown in Figure is the root It must be readable and by the In addition to the files, the 26: Sessions are located here. Because is the only library that is used, it is stored in the main Chat directory along with the 26.3 Chat Script Directory Structure And is the main CGI script that performs all the chat room and posting new chat messages. This file must and is the setup file for the chat.cgi script. This file must contains Perl code that has various routines to related to the chat script. This file must be Sessions is used by the chat script to store the to each user’s session after he or she first logs on to the This directory must be readable, writable, and Chat_open is used by chat.cgi to store messages for the Open Forum chat room. This is one of the chat rooms set up in the file. This directory must be readable, writable, and Chat_CGI is used by chat.cgi to store messages for the CGI talk chat room just as Chat_open does for the Open Forum . This directory must be readable, writable, and Directory (read, (read, (read, write, (read, write, (read, write, 26: addition to the chat messages, the various chat room store “who” files that contain about who is each chat room. The chat script generates and deletes these so you do not need to bother with Setup and file contains the variables for chat.cgi. is a list of these setup is a list of chat room names. These names are of the rooms that are available on the chat. For if you had one chat room for open topics and another CGI this variable would be set to ("Open "CGI Chat is an array that contains the directory names the list of rooms in Each of these the chat messages related to its own room in is a list of form variable names related to each Whenever chat.cgi is called after the initial logon, chat.cgi must have the variable chat_room sent to it. This variable should be equal to the name in the array. Each element of this array each chat room listed in the array. Because the values here are variable names, you should use lowercase text, instead and no special is the chat.cgi script name. Most systems keep this set to but some systems rename the script. For Windows NT Web servers require the script name to be changed to a .bat The variable is used by chat.cgi to to itself from a is set to on to filter HTML out of a user’s messages. It is a good idea to prevent users from posting HTML within their messages, can do nasty things such as leave off a closure 26: as </H1> if they are including a header), extending the rogue tag to all the other messages. Figure 26.4 shows an example of how HTML tags look after they are 26.4 WebChat with HTML filtering is set to on if you want to prevent people from images in their messages. Setting $no_html to on also filters out HTML tags, so setting this variable to on is for who want to continue to allow HTML posting but not images. In other filters all HTML tags, including image tags, so not be set if you have already $no_html to be is the location of the directory where the session files are stored for chat users. When users log into the chat area, a session file so that they do not have to keep their 26: is the time in days that session files stay being deleted. This value can be a fraction. For example, a value of .25 would delete sessions every quarter day (six Perl is actually the setup file, you can use a for- mula instead of a standard value. A formula can be easier to read and maintain. For example, "1/24" (1 divided by 24) is a one- hour time frame. "1/24/12" (1 divided by 24 divided by 12) is a time is the time in days that the who files stay active. Who who is active in a given chat room at a given time. This value can Ideally, it should be very short. Using the value "1/24/12" by 24 divided by 12) means that the who files stay around for about five minutes before being removed. A user can always “leave” a chat room by going to another WWW page on the Internet, and this act is not to be sent to the chat script. If the who files often enough, they provide a accurate way of who is currently in the system. Who files are refreshed whenever a user refreshes the chat messages or submits a message to the chat is on if you want a message to post when someone enters a room. This message usually announces to in the room that the user has logged is the number of days after which a chat mes- sage is too old to leave on the system. These messages If this variable is set to zero, the chat messages will not on the basis of age. This number may be For setting it to ".25" will delete messages older than six is the maximum number of messages you want to leave on the system before the oldest ones are deleted. For if you specify this number to be 10, then only 10 messages will per chat room. In this case, after the 11th message is number 1 is deleted. Setting this value to zero means that you do not want any messages deleted on the basis of a maximum number to keep on the 26: a real-time chat system, it is that you set up the sys- tem to keep few messages around. For one thing, in a real-time after about five or 10 minutes, people have probably moved on to another topic. Also, the chat script operates more if it does not have to process so many messages in the chat following is an example of all the setup variables in the = ("CGI "Open = = ("cgi", = = = = = = = = = OTHER SETUP chat script has the ability to reference another setup file in case file does not meet the needs of every chat room. although multiple chat rooms, you may want to assign a different automatic removal of messages policy for each one. In the Open Chat Room, you may want to delete than five minutes, but in the CGI chat room, you may not want to delete any can do this by using another setup file that is loaded with the same variables defined in is always loaded by script. However, if you send the setup variable on the URL link 26: script as a variable, chat.cgi will read a setup file on the basis of that variable. For example, if you specified the call to chat.cgi file would be loaded by the chat script after the is is always This means that if you choose using the setup form variable, you need the variables you want changed in the new setup file instead of all the variables residing in THE HTML The HTML used by the chat script is stored in the file. This Perl script outputs the various HTML forms for viewing and posting To modify the cosmetics of the chat script, you need only edit this file. The structure of this script is discussed in more detail in the chat script allows the user to choose frames versus view of the chat script, the Perl code that generates the HTML for printing to the user’s Web browser can seem a bit If you plan to edit the HTML for the chat script, you the “Design of Also, as usual, make a backup of any files you are planning to edit so that you can go back to the original if anything becomes messed up is the use chat.cgi, you simply call it by itself. A sample URL for this script, if it is installed in the Chat the cgi-bin The chat program prints an HTML form 26: user to log on to a chat room. An example of this form is displayed in Figure 26.5. However, if you are using a setup file other than need to specify this in the URL that you use to run the chat example of this setup file was 26.5 Example of chat room ENTRANCE FORM the chat room logon screen shown in Figure 26.5, there are variables that the user is asked to fill out. These variables affect how the rest of the chat session is is the alias that the user will be referred to in the This field must be address is the E-mail address of the user. This is optional. If the user chooses to fill in this variable, a MAILTO hypertext reference tag will be displayed whenever the user’s name is shown in the chat 26: page is the URL of the home page of the user. This field is If the user chooses to give the home page URL, a hypertext will be displayed whenever the user’s name is shown in the many old messages to display how many old are displayed along with the new messages whenever the chat are loaded. an overlap of about 10 old messages is good for the of a If you see only new and if they refer to a topic discussed it is harder for to visualize how the is flowing. Seeing a couple of the old messages serves as a reminder. This is true in where many topics might be discussed at rate is the number of seconds before the browser reloads the script to display the new messages. This field is for browsers that support the META refresh tag. Setting this field to zero disables automatic of the check box for using frames is turned on, be used to display messages in one frame while the form for a post is displayed in another frame. An example of frames was shown in Figure chat room variable allows the user to select the chat room chat performs all its functions inside chat.cgi. include the listing of messages in a chat room as well as of those messages. Depending on the value of incoming chat.cgi which procedure to perform. A basic of the Web chat features is shown in Figure 26: 26.6 Basic flow chart for the Web first line of the following code sets up the location of the to the program. By default, $lib is set to the current Then the library (for form parsing the setup file for and the Perl library the HTML code for the chat = incoming form variables are read to the %in array using the ReadParse Everyone & View & View Occupants & View 26: