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

Thể loại: Hệ điều hành
Lượt xem: 13,095Lượ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 6 WebChat OVERVIEW WebChat is a useful CGI program that allows a number of people on the World Wide Web to talk to one another simultaneously. It differs from a BBS (bulletin board system), in which the messages are typically read hours 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 like. Although both WebChat and WebBBS store messages for other peo- ple to read, there is a major difference in how the user sees and posts messages. The BBS emphasizes long-term storage of messages, including statistical data such as the date and time the message is posted. The BBS also encourages users to branch out into different topics in “threads” of replies. On the other hand, WebChat emphasizes the quick posting of small messages much like a conversation among a group of people. Dialogue is designed to flow swiftly in small, easily digested chunks. Additionally, because the topic is being discussed by everyone at the same time, there is little room for different people discussing many different things in the same chat session. Thus, there is no reason to keep track of different threads of conversation. Figure 26.1 An example dialogue in WebChat. Because people are discussing things simultaneously in real time, another feature of WebChat is the ability to refresh or display new mes- sages as quickly as desired. This is done using the META HTML tag to force refreshes within a certain time frame. WebChat includes many features designed to facilitate this kind of dialogue. In WebChat, users can refresh messages using a button that is Chapter 26: WebChat 684 displayed in plain view. In addition, if the user is using a browser such as Netscape, that supports the META REFRESH HTML tag, the user can choose to have the chat messages refresh or redisplay themselves automatically at a user-defined interval. Messages are displayed in chronological order of posting from most recent to oldest so that users can quickly look through a list of state- ments. In addition, users can specify whether to see only new messages each time they refresh the screen or to include a user-defined number of previous messages. Viewing several of the previous posts along with new ones tends to provide the user with greater continuity. By default, messages are posted to everyone, and the user’s informa- tion is embedded as part of a posted message. This arrangement facili- tates quick posting. By default, posted messages are seen by everyone. However, 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 to someone whispering a comment to someone else in the middle of a larg- er meeting. Additionally, Netscape-style frames are supported; messages are refreshed in one frame while the user types messages in another frame. This feature allows a user to set a relatively high refresh rate for seeing new messages, while leaving the message submission form intact while the user is typing a message. Figure 26.2 shows an example of WebChat with frames. WebChat also has configurable options such as the automatic announcement 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 leaves, he or she is encouraged to announce the departure by pressing the Log Off button. Nothing is more disturbing than to find out the person you were chatting with has left the room! In addition, WebChat can be customized to remove old messages by age and by number of messages. For example, if WebChat is used for real- time conversations, it is generally not useful to keep the conversation messages for more than an hour. Additionally, you may want to make Chapter 26: WebChat 685 sure that not more than 10 or 20 messages stay around at any given point, because messages older than the first 10 may be irrelevant to the current course of conversation. On the other hand, on other chat areas, you may want to keep the messages around for a long time to keep a full transcript of the discussion or meeting. Figure 26.2 WebChat with frames on. INSTALLATION AND USAGE The chat files on the accompanying CD-ROM will install into a directory called Chat. The files and subdirectories associated with this application along with their required permissions are shown in Figure 26.3. Chat is the root directory. It must be readable and executable by the Web server. In addition to the application files, the Chat_open, Chat_CGI, Chapter 26: WebChat 686 and Sessions subdirectories are located here. Because CGI-LIB.PL is the only non–application-specific library that is used, it is stored in the main Chat directory along with the application. Figure 26.3 Chat Script Directory Structure And Permissions. chat.cgi is the main CGI script that performs all the chat room functions, including displaying and posting new chat messages. This file must be readable and executable. chat.setup is the setup file for the chat.cgi script. This file must be readable. chat-html.pl contains Perl code that has various routines to output HTML related to the chat script. This file must be readable. The Sessions subdirectory is used by the chat script to store the files related to each user’s session after he or she first logs on to the chat room. This directory must be readable, writable, and executable. The Chat_open subdirectory 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 default chat.setup file. This directory must be readable, writable, and executable. The Chat_CGI subdirectory 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 executable. Chat Directory (read, execute) chat.cgi (read, execute) chat.setup (read) chat-html.pl (read) cgi-lib.pl (read) Chat_open Subdirectory (read, write, execute) Chat_CGI Subdirectory (read, write, execute) Sessions Subdirectory (read, write, execute) Chapter 26: WebChat 687 In addition to the chat messages, the various chat room directories also store “who” files that contain information about who is currently in each chat room. The chat script generates and deletes these files automatically, so you do not need to bother with maintaining them. Server-Specific Setup and Options The chat.setup file contains the configuration variables for chat.cgi. The following is a list of these setup items. @chat_rooms is a list of chat room names. These names are descriptive names of the rooms that are available on the interactive chat. For exam- ple, if you had one chat room for discussing open topics and another for discussing CGI programming, this variable would be set to ("Open Chat Room", "CGI Programming Chat Room"). @chat_directories is an array that contains the directory names that match the list of rooms in @chat_rooms. Each of these directories stores only the chat messages related to its own corresponding room in the @chat_rooms array. @chat_variable is a list of form variable names related to each chat room. 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 @chat_variable array. Each element of this array corresponds to each chat room listed in the @chat_rooms array. Because the values here are variable names, you should use lowercase text, underscores instead of spaces, and no special characters. $chat_script is the chat.cgi script name. Most systems keep this vari- able set to "chat.cgi," but some systems rename the script. For example, some Windows NT Web servers require the script name to be changed to a .bat extension. The $chat_script variable is used by chat.cgi to make references to itself from a URL. $no_html 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, because they can inadvertently do nasty things such as leave off a closure tag Chapter 26: WebChat 688 (such 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 messages with HTML tags look after they are filtered. Figure 26.4 WebChat with HTML filtering activated. $no_html_images is set to on if you want to prevent people from referenc- ing images in their messages. Setting $no_html to on also filters out image- related HTML tags, so setting this variable to on is for administrators who want to continue to allow HTML posting but not images. In other words, $no_html filters all HTML tags, including image tags, so $no_html_images need not be set if you have already configured $no_html to be on. $chat_session_dir 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 is created so that they do not have to keep respecifying their information. Chapter 26: WebChat 689 $chat_session_length is the time in days that session files stay active before being deleted. This value can be a fraction. For example, a value of .25 would delete sessions every quarter day (six hours). Because Perl is actually processing the setup file, you can use a for- mula instead of a standard fractional 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 five- minute time frame. $chat_who_length is the time in days that the who files stay active. Who files show who is active in a given chat room at a given time. This value can be fractional. Ideally, it should be very short. Using the value "1/24/12" (1 divided 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 of leaving is not guaranteed to be sent to the chat script. If the who files are deleted often enough, they provide a relatively accurate way of determin- ing who is currently in the system. Who files are refreshed whenever a user refreshes the chat messages or submits a message to the chat room. $chat_announce_entry is on if you want a message to automatically post when someone enters a room. This message usually announces to every- one in the room that the user has logged on. $prune_how_many_days is the number of days after which a chat mes- sage is considered too old to leave on the system. These messages are deleted. If this variable is set to zero, the chat messages will not be removed on the basis of age. This number may be fractional. For exam- ple, setting it to ".25" will delete messages older than six hours. $prune_how_many_sequences is the maximum number of messages you want to leave on the system before the oldest ones are deleted. For exam- ple, if you specify this number to be 10, then only 10 messages will be allowed per chat room. In this case, after the 11th message is posted, message 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 of messages to keep on the system. Chapter 26: WebChat 690 For a real-time chat system, it is recommended that you set up the sys- tem to keep few messages around. For one thing, in a real-time conver- sation, after about five or 10 minutes, people have probably moved on to another topic. Also, the chat script operates more efficiently if it does not have to process so many messages in the chat directory. The following is an example of all the setup variables in the chat.setup file. @chat_rooms = ("CGI Programming", "Open Forum"); @chat_room_directories = ("Chat_CGI", "Chat_Open"); @chat_room_variable = ("cgi", "open"); $chat_script = "chat.cgi"; $no_html = "off"; $no_html_images = "off"; $chat_session_dir = "Sessions"; $chat_session_length = 1; $chat_who_length = 1/24/12; $chat_announce_entry = "off"; $prune_how_many_days = .25; $prune_how_many_sequences = 10; USING OTHER SETUP FILES The chat script has the ability to reference another setup file in case the default chat.setup file does not meet the needs of every chat room. For example, although chat.setup accommodates 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 messages older than five minutes, but in the CGI programming chat room, you may not want to delete any messages. You can do this by using another setup file that is loaded with the same variables defined in chat.setup. chat.setup is always loaded by the chat.cgi script. However, if you send the setup variable on the URL link to Chapter 26: WebChat 691 the script as a Url-encoded variable, chat.cgi will read a setup file on the basis of that variable. For example, if you specified the call to chat.cgi as http://www.foobar.com/cgi-bin/Chat/chat.cgi?setup=test the test.setup file would be loaded by the chat script after the chat.setup file is loaded. chat.setup is always necessary. This means that if you choose to override chat.setup using the setup form variable, you need specify only the variables you want changed in the new setup file instead of all the variables originally residing in chat.setup. MODIFYING THE HTML The HTML used by the chat script is stored in the chat-html.pl file. This Perl script outputs the various HTML forms for viewing and posting chat messages. 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 “Design Discussion” section. Because the chat script allows the user to choose frames versus a nonframes view of the chat script, the Perl code that generates the HTML for printing to the user’s Web browser can seem a bit com- plex. If you plan to edit the HTML for the chat script, you should study the “Design Discussion” of chat-html.pl. Also, as usual, you should 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 after something is changed. Running the Script To use chat.cgi, you simply call it by itself. A sample URL for this script, if it is installed in the Chat subdirectory underneath the cgi-bin directory, follows. The chat program automatically prints an HTML form asking Chapter 26: WebChat 692 the 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 chat.setup, you need to specify this in the URL that you use to run the chat script. An example of this alternative setup file was previously discussed. http://www.foobar.com/cgi-bin/Chat/chat.cgi Figure 26.5 Example of chat room logon. CHAT ENTRANCE FORM VARIABLES In the chat room logon screen shown in Figure 26.5, there are several form variables that the user is asked to fill out. These variables affect how the rest of the chat session is processed. Username is the alias that the user will be referred to in the chat room. This field must be entered. Email 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 room. Chapter 26: WebChat 693 Home page is the URL of the home page of the user. This field is also optional. If the user chooses to give the home page URL, a hypertext ref- erence will be displayed whenever the user’s name is shown in the chat room. How many old messages to display determines how many old mes- sages are displayed along with the new messages whenever the chat mes- sages are loaded. Generally, an overlap of about 10 old messages is good for maintaining the continuity of a conversation. If you see only new mes- sages and if they refer to a topic discussed previously, it is harder for most people to visualize how the conversation is flowing. Seeing a couple of the old messages serves as a reminder. This is especially true in chat rooms where many topics might be discussed at once. Refresh rate is the number of seconds before the browser automati- cally reloads the script to display the new messages. This field is useful only for browsers that support the META refresh tag. Setting this field to zero disables automatic refreshing of messages. If the check box for using frames is turned on, Netscape-style frames will be used to display messages in one frame while the submission form for a post is displayed in another frame. An example of frames was shown in Figure 26.2. The chat room variable allows the user to select the chat room to enter. DESIGN DISCUSSION The chat application performs all its functions inside chat.cgi. These operations include the listing of messages in a chat room as well as the creation of those messages. Depending on the value of incoming form variables, chat.cgi determines which procedure to perform. A basic flow- chart of the Web chat features is shown in Figure 26.6. Chapter 26: WebChat 694 Figure 26.6 Basic flow chart for the Web chat. Chat.cgi The first line of the following code sets up the location of the supporting files to the program. By default, $lib is set to the current directory. Then the cgi-lib.pl library (for form parsing routines), the setup file for the script, and the Perl library containing the HTML code for the chat script are loaded. $lib = "."; require "$lib/cgi-lib.pl"; require "./chat.setup"; require "./chat-html.pl"; The incoming form variables are read to the %in associative array using the ReadParse subroutine. &ReadParse; Send Message To Everyone & View Messages View Messages Logoff Send Private Message & View Messages View Occupants & View Messages Enter Chat Logon Screen Chapter 26: WebChat 695