Chat using ASPs and DB and the Scripting.FileSystemObject

By ServerWatch Staff (Send Email)
Posted Jul 6, 1999

Joco Vieira

In this code let me say that it really works well, but the chat will be slow when you get too many users.

In this code let me say that it really works well, but the chat will be slow when you get too many users.

1. First you must create an DataBase (the place where you will have the messages that will appear)

The structure of my DataBase is only one table :

chatmain(id As Autonumber, data As Date/Time, nick As Text,message As Text)

(ex: table(field1 As type, field2 As type) )

in this example there is no control of the nick names, but its easy, so I think that you will handle it good by yourself

2. I'm using some images , just to make the chat with a good look, nothing but a banner, that you can use for advertising.

3. You will have then to make the code, I use only 3 files, but the chat itself use 4, one of the files is generated by the asp file, so you will learn how to create text files on-line with ASP

FILE: chat.htm

This is the file that controls the frames

<title>Your Chat</title>

<frameset framespacing="0" border="false" frameborder="0" rows="65,58%,*">
  <frame name="top" scrolling="no" noresize target="middle" src="titulo.htm">
  <frame name="middle" target="bottom" src="top.htm" scrolling="auto"> ' top.htm is the file that asp file will generate
  <frame name="bottom" src="bottom.asp" scrolling= "no" noresize> <noframes> <body> <p>This page uses frames,
but your browser doesn't support them.</p> 
</noframes> </frameset> 

at this time we have the
main page.... the page were people will have to get in to
access the chat site
FILE: titulo.htm
This file represents the top page of your frames chat, its another easy file that will only have the banner with the link.
<base target="middle">
<title>Your Chat</title>
<body topmargin="0" leftmargin="0">
<p align="center"><a href="http://www.companyname.com" target="_blank"><img
src="images/banner.gif" width="280" height="62" alt border="0"></a></p>
Now, all we have to do is the more important part, the ASP file that will control all the chat...
FILE: bottom.asp

This file will use some features of ASP that I really like, the Scripting Object that gives you the chance 
to create any kind of text file, and the ADO, that we all use to access ODBC DB with the DSN...
Don't be scared with the size of the code, remember that this file will do a lot of work in the Chat.
<%@ Language="VBSCRIPT" %>
      dim data ' this will carry the Date
      dim name ' here we will have the nickname
      dim message ' this is the variable that will carry with the sent message
      dim error ' i use this variable to control if theres errors
      dim asn ' with this I will control the bad language used on the chat
      error = "" ' lets start the error, with no error
      data = now() ' the Date will be equal with the today date
      name =  left(request.form("name"),10) , we only accept nicknames with 10 or less letters
      message = request.form("message") ' we get the message from the form field message
      asn = InStr(1, message, "f**k", 1) + InStr(1, message, "c*nt", 1) + InStr(1, message, "sh*t", 1) 
      if request.querystring("name2") = "" then ' this is how i see if the person is new in the chat
            psw = 0
      end if
      if name = "" then ' if theres no nickname, then it will be Guest
         name = "Guest"
      end if
      if (message = "" or message = " ") and Request.querystring("vezes") = "1" then ' this is how I validate the message, it can't be empty
          error = " Please enter a valid message..."
      end if
      if CInt(asn) > 0   then ' if asn is a number bigger than 0 then there bust be some bad language at the message
          error = " Please no bad language..." ' this will be the error message
      end if
      if message <> "" And messagec <> message then ' messagec controls if the user don't send equal messages
           Set Conn = Server.CreateObject("ADODB.Connection")
           conn.open"DSN=chatdb;" ' i named my DSN with chatdb, you can use any, but don't forget to change it here too
           Set rs = Server.CreateObject("ADODB.Recordset") ' create the record set
           SQLStmt = "SELECT * FROM chatmain" ' select everything from the chatmain table
           rs.Open SQLstmt,Conn,1,2
           rs.AddNew ' lets add a new message to the DB
           rs("nick") = name ' put the nickname
           rs("message") = message ' the message
		   session("messagec") = message ' don't forget this, so you will late control if they are equal
		   rs("data") = now ' date and time
		   Set FileStreamObject = CreateObject("Scripting.FileSystemObject") ' this is how you create the scripting object
                   Set writeStream = FileStreamObject.CreateTextFile(Server.MapPath("top.htm"), True) ' here you set the name of the file that will be created
	           Set rs2 = Server.CreateObject("ADODB.Recordset") ' i've opened a new recordset because I had some problems with the other rs
                   SQLStmt = "SELECT * FROM chatmain ORDER BY id DESC"
                   rs2.Open SQLstmt,Conn,1,2
		   writeStream.WriteLine "<html>" ' this is the command that writes to the file
		   writeStream.WriteLine "<head>" ' as you see  i am creating an html file
		   writeStream.WriteLine "<meta http-equiv='Refresh' content='25; URL=top.htm'>" ' this is the refresh that will ensure that the file is updated to the user of the chat
		   writeStream.WriteLine "<title></title>"
		   writeStream.WriteLine "<base target='footnotes'>"
		   writeStream.WriteLine "</head>"
		   writeStream.WriteLine "<body topmargin='0' leftmargin='0'>"
		   writeStream.WriteLine "<div align='left'>"
		   writeStream.WriteLine "<table border='0' width='100%'>"
		   writeStream.WriteLine "<tr>"
		   writeStream.WriteLine "<td width='100%' valign='top'><div align='center'><center><table border='0' width='98%' cellspacing='0' cellpadding='0' height='25'>"    
		   writeStream.WriteLine "<FONT COLOR='#CC0000'><small>" & Application("ActiveUsers") & " chatting<small></FONT> <br>"
		   count = 0 ' I use this counter to ensure that won't be no more than 60 messages at the same time
		   Do while not rs2.eof and count < 60 ' like I said the control of the messages, and the control of the end of the data
        		   ' here I will write the data that was written in the DB, so everyone that access
			   ' to the top.htm will see the messages, simulating a real chat program
                           writeStream.WriteLine "<tr border='0'>"
                           writeStream.WriteLine "<td width='13%' height='4' valign='top'><small><small><small><strong>&nbsp;" & formatdatetime(rs2("data"),4) & "</strong></small></small></small></td>"
                           writeStream.WriteLine "<td width='20%' height='4' valign='top'><font color='#800080' size='1'><strong>&nbsp;" & rs2("nick") & "</strong></font></td>"
                           writeStream.WriteLine "<td width='67%' height='4' valign='top'><font face='Arial' color='#000080' size='1'><strong><small>&nbsp;" & rs2("message") & "</small></strong></font></td>"
                           writeStream.WriteLine "</tr>"
		      count = count + 1
			writeStream.WriteLine "</table>"
			writeStream.WriteLine "</center></div></td>"
			writeStream.WriteLine "</tr>"
			writeStream.WriteLine "</table>"
			writeStream.WriteLine "</div>"
			writeStream.WriteLine "<p></p>"
			writeStream.WriteLine "</body>"
			writeStream.WriteLine "</html>"
        	        session("messagec") = "0"
	   end if
<body bgcolor="#000000" topmargin="0" leftmargin="0">
<script language="JavaScript">
<div align="left">
<table border="0" width="90%">
    <td width="100%"><table border="0" width="71%" height="106">
        <td width="33%" height="102"><form method="post" name="ngchatform" action="bottom.asp?vezes=1">
          <font color="#ff8000" face="Arial"><strong><p><small><small>Name : </strong></font><input
          name="name" value="<%=name%>" size="11"><br>
          <font color="#ff8000" face="Arial"><strong>Message :<small> </strong></font><input
          name="message" size="20" value="<%=message%>"> <input type="submit" value="Send"
          name="Send"><input type="reset" value="Reset" name="Reset">  </small></small></small></p>
        <td width="33%" height="102" valign="top"><p align="center"><small><small><font
        color="#008000">Comments to<br>
        <a href="mailto:developer@joaovieira.com">developer@joaovieira.com</a><br>
        </font></small></small><br><small><font color="yellow"><%=error%></font></small></td>
<% if session("come") <> "true" then ' this is the way I use to see if it is the first or second come from the user
                                      ' if it is not true so it is the first come, i will
                                      ' put the focus on the nickname, else, I clean the message
                                      ' and put the focus on the message.  %>
  <script language="JavaScript">
		// -->
     <script language="JavaScript">
		// -->
<%end if
session("passou") = "true"

Page 1 of 1

Comment and Contribute

Your name/nickname

Your email

(Maximum characters: 1200). You have characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date