Chat room self-made Follow Me

Source: Internet
Author: User
Tags define array chr empty client server memory
Chat room to the internet and not to the chat room of the people, is very rare, below, we will make a simple chat room.
There are usually two ways in which a chat room displays a conversation. One is to refresh the entire dialog display area every x seconds, the disadvantage of this side is obvious: large data transmission and the screen has a flicker; second, there is a new speech, the content is passed to the client, and added at the end of the original dialogue, this method of data transmission is relatively small and the screen is smooth scrolling, good visual effect. The implementation of the first method already has a lot of information to introduce, so this article describes the second way to refresh.
Design ideas:
The ⑴ server side only retains the latest 30 statements as a buffer, and the new speeches will squeeze out the oldest speeches.
⑵ each sentence is assigned an incremented ordinal number. The number of statements that the client last appears is recorded in the session.
⑶ use the hidden frame for refreshing, refresh, compare the session value to the number of each statement in the buffer, and if the ordinal is greater than the session value to indicate that the statement is new, take the statement's data out and assign the statement's ordinal value to the session.
When the ⑷ dialog display area is opened with Document.open (), it is no longer closed, and when new data is fetched and uploaded to the client when the hidden frame is refreshed, the raw data is formatted for display (this work can be performed on the server side, but to ease the burden on the server, Transfer the synthesis of the conversation to the customer, and then call the Document.writeln method to write the contents of the speech to the end of the dialog display area.
⑸ in the dialog display area to place a section of automatic scrolling code, so that the screen automatically scroll down, and through the check box of the speech area to choose whether to automatically scroll.
Implementation method:
One, this procedure only needs 4 files to realize:
⑴chat.asp: Chat Room frame file, responsible for initializing the program operating environment and randomly allocate four digits as user name, then the page from top to bottom into four parts, top and bottom two frame height of 0.
⑵chatfyq.htm: Speaking area, providing a text box for entering names, statements, and a spoke button and an automatic scrolling switch (check box), and defining Javascript:checksays () performed on the client (validity detection of statements), CLS () (Empty the contents of the dialog display area), write () (Initialize dialog display area, define page color, text size, and place scrolling code), SW (username) (when clicking on the name in the conversation area, write the name to the speaker box), W (UN,SA,TN,DT) (Combine the original data into a complete statement and add it to the end of the dialog display area).
⑶chatt.asp: This file is located at the top of the hidden frame (height 0) in a timed refresh, the new statement of raw data used <script LANGUAGE=JAVASCRIPT&GT;PARENT.F2.W ("name", "Speech content", "original name" , "Speak Time") </script> format output to the client, you can invoke the chatfyq.htm defined in the W (UN,SA,TN,DT) function to display the speech content.
⑷CHATSAY.ASP: Statement submitted to this file for processing. This file is located in the hidden frame at the bottom. After adding the statement to the buffer, immediately executes the same code snippet as chatt.asp and displays the statement. (The code behind the chatsay.asp is the same as the chatt.asp, which is not invoked after chatsay.asp using Response.Redirect "chatt.asp" to speed up, Because the redirection method needs to establish two times the connection: The speech submission is the connection once, redirects once again to connect again, the efficiency is low. )
Second, some key code comments:
⑴ Initialization Code Snippet: Application ("Chat_hh") the last sentence in the buffer, the serial number, session ("CHAT_HH") is the session value mentioned in "Design idea ⑶", which is used to record the number of the last sentence that the client has displayed:


' If the ordinal is empty, the program is not initialized
If application ("chat_hh") = "" Then
' Define buffer array, a total of 30 statements
' Each statement occupies 5 positions: Serial number, name, statement, original name, speaking time
Dim SD (150)
Application.Lock
' Assign the array to application (' CHAT_SD '), that is, the speech remains in server memory only
Application ("CHAT_SD") = SD
' Defines an initial ordinal number of 0
Application ("chat_hh") = 0
Application.UnLock
End If
' To avoid a large serial number, reset the serial number to 0 whenever the serial number increases to 65535.
If application ("Chat_hh") > 65535 Then
Application.Lock
Application ("chat_hh") = 0
Application.UnLock
End If
' The session that the client uses to record which sentence has been displayed ("Chat_hh")
' Its value equals the ordinal minus 12, that is: The new user can see the original 12 statements
Session ("chat_hh") = Application ("Chat_hh")-12


JavaScript functions in ⑵chatfyq.htm: Write ():


function Write () {
Use Document.open () to open the dialog display area, open it and not close it, so you can keep adding new content to it
Parent.f1.document.open ();
Writing HTML file headers to the dialog display area
Parent.f1.document.writeln ("Write style sheet definition
Parent.f1.document.writeln ("<style type=text/css>.t{color:aaaaaa;font-size:9pt;}" Body{font-family:\ "song body \"; Font-size:10.5pt;line-height:160%}a{text-decoration:none}a:hover{text-decoration: Underline}a:visited{color:blue}</style>Write automatic scrolling code
//...... (Note: The code omitted here is shown in the complete source program)
Parent.f1.document.writeln ("StartUp ();</\script>");
Parent.f1.document.writeln ("<body bgcolor=cde0fc text=000000>");
Parent.f1.document.writeln ("Load the chatt.asp into the hidden frame at the top of the frame and perform a timed refresh function
Parent.t.location.href= "chatt.asp";
}


⑶chatt.asp timed to refresh and get the latest statement of the code:


' Cache the conversation contents of the chat room in the SD array
SD = Application ("CHAT_SD")
' Get the serial number of the last statement shown
USERHH = Session ("CHAT_HH")
' Define the show array for the content you want to display
Dim Show ()
ReDim Preserve Show (0)
j = 1
NEWUSERHH = 0
' Iterate over the buffer of speech content
For i = 1 to 5
NEWUSERHH = SD (i)
' If SD (i) (the serial number of each statement) is greater than the serial number of the final statement of the client and the statement exists, it is stored in the show array
If SD (i) > userhh and SD (i) > 0 Then
ReDim Preserve Show (J), Show (j + 1), show (J + 2), show (J + 3)
Show (j) = SD (i + 1)
Show (j + 1) = SD (i + 2)
Show (j + 2) = SD (i + 3)
Show (j + 3) = SD (i + 4)
j = j + 4
End If
Next
' Output JavaScript to client
Response.Write "<script language=javascript>"
' Output the contents of the show array
For i = 1 to UBound (show) Step 4
' Output PARENT.F2.W (show (i), display (i+1), shows (i+2), showing (i+3), invoke the W (chatfyq.htm) function of the client UN,SA,TN,DT to indicate the contents of the speech
Response.Write "PARENT.F2.W (" & Chr) & Show (i) & Chr (+) & "," & Chr (+) & Show (i + 1) & Ch R & "," & Chr (+) & Show (i + 2) & Chr (+) & "," & Chr (+) & Show (i + 3) & CHR ; "); &qu



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.