Best Practices | Knowledge Management | Security | Tutorials | Books | Tools

Intranet Journal

Home | Exchange | FAQ | Software & Standards


Simple ASP Chat


By Sanjay Shetty

Creating the Main Form
To start with create a simple form where the user can type some text. Whatever the client types in this form has to be appended to a text file, this text file will then be displayed to the client. The actual writing to the text file is done with an Active Server Script.
 	<FORM method="post" action="chat.asp">
<Input type=text value="" size="35" name="txtbox">
<Input type=submit value="GO">
Refreshing the Page
By including the meta tag for refreshing the page will be refreshed every 5 seconds. This concept is called client pull. This is because it is the clients responsiblity to read the meta tag and call for a refresh every five seconds.
<HEAD>
<META http-equiv="refresh" content="5">
</HEAD>

The form for submitting the data and the page for reading the text are on two different pages. In order to combine them we put both pages with in a frame. This allows the user to easy type and read the responses without being interrupted.

Installation is fairly simple, just follow these steps:
  1. Create a virtual root called "asp", make it giving it Execute Access
  2. Download and expand ftp://ftp.15Seconds.com/012497.zip (1064 bytes) into the virtual directory just created.
  3. Create a blank text file in that directory called textwork.txt

If you want to change the name of virtual root, you must change the name in the Active Server Page code since it uses the virtual root name to find the text file's path.

Mainchat.asp could be mainchat.htm, and located in a directory with read access but the installation is easier with all the files in the same directory.

The Active Server Page
Since the above form is in a file called chat.asp and the form posts to chat.asp, the form post to itself. To handle the output of the post you need some Active Server Code.

<%

textstr=Request.Form("txtbox")
Set FileObject = Server.CreateObject("Scripting.FileSystemObject")
TestFile = Server.MapPath ("/asp") & "\textwork.txt"
Set OutStream= FileObject.OpenTextFile (TestFile, 8, TRUE)
ipaddr=Request.ServerVariables("REMOTE_ADDR") & " : "
OutStream.Write ipaddr
OutStream.WriteLine textstr & "<BR>"
Set OutStream = Nothing
%>

Active Server Page
<%
Set FileObject = Server.CreateObject("Scripting.FileSystemObject")
TestFile = Server.MapPath ("/asp") & "\textwork.txt"
Set InStream= FileObject.OpenTextFile (TestFile, 1, False, False)
Response.Write Instream.ReadALL & "<BR>"
Set Instream=Nothing
%>

Here a Fileobject is created in the READ mode by specifying the parameter 1 to the OpenTextFile() method. In the following statement two things are done in one stroke. The READALL() function is used to read the entire contents of the file and then the Response.Write function is used to output the entire file to the client. The final step it to close the object.

Simple ASP Chat
This issue focuses on the use of Active Server Pages to dynamically create a chat page. The converstaion is stored in a file object. The chat pages are contained with in a framed page broken into two sections. The upper frame is the main view of the converstaion. The lower frame allows the viewer to input a line to the conversation. The upper frame refreshes based on a Meta refresh tag, each time reading the file and displaying it's contents. The lower frame writes a line of converstaion to the file.
Explaination
The first line of code uses the IIS provided Request object to find the value present in the txtbox that is filed in the form.
textstr=Request.Form("txtbox")< /FONT>
The value is stored in the textstr variable.

Next the Fileobject is opened it's reference is stored in the Fileobject variable.

Set FileObject = Server.CreateObject("Scripting.FileSystemObject")
TestFile = Server.MapPath ("/asp") & "\textwork.txt"
Then we specify the file name and it's path and store it in a variable called Testfile. Here we have a file called textwork.txt to which all the text of the chat is appended.

Next step is to open this file in append mode by specifying the number 8 to the OpenTextFile method.

Set OutStream= FileObject.OpenTextFile (TestFile, 8, TRUE)
ipaddr=Request.ServerVariables("REMOTE_ADDR") & " : "
The other thing that is being done in this section is storing the ip address of the client to a variable and appending the string " :" to it.

OutStream.Write ipaddr
Writes the IP address of the client to the file.

OutStream.WriteLine textstr & "<BR>"
Writes the text written by the client and appends the BR tag. This tag is added so that when we display the chat session to the clients with each text row on a new line.

Once having done that the stream is closed that is opened to the file.

Set OutStream = Nothing< /FONT >


Next: Creating Excel WorkSheets with ASP

Index: Active Server Pages\IIS

About the Author
Sanjay Shetty, was doing Computer Eng.. but dropped out of a college. Got involved in the HARE KRISHNA culture meanwhile started Nirvana Software Consultancy in 1994 a firm which specializes in database application software development using FoxPro on the Windows platform. In 1996 started another company, DBS Internet Services along with a couple of other friends and colleagues. The company specializes in creating and hosting internet/intranet web sites and also specializes in developing internet/intranet applications.

Copyright 2002 Jupitermedia Corporation, All Rights Reserved.
Legal Notices | Licensing, Reprints, & Permissions | Privacy Policy | Advertising on Intranet Journal
Home | eXchange | F A Q | Find | Register |