PDA

View Full Version : ASP Chat Script - Part 1 of 3


admin
06-10-2003, 03:39 PM
By Kumar Shanmuganathan

INTRO

Many of you people out there will be wondering how a chat program functions, well through this article I will illustrate this to you. With the help of the new powerful web programming language Active Server Pages (ASP) and with the aid of the scripting language VBScript this can be done very easily as shown below.

OVERVIEW

In order to create a simple chat program, we will need to collect 2 pieces of data. They are both alphanumeric data. That is the "Chat name" with which the user wishes to chat with and "User Messages", these of course are the messages that the user sends to other users to chat with. Then we need to store these two pieces of data somewhere for later access. Then we will have to use these two pieces of data and display them, as the user will be entering the messages simultaneously. Finally, we will have to logout the user and display messages on the active chat window, displaying messages about user's entry and exit. Well let's get started and witness how this works.

Part 1

Firstly lets get the user's chat name and store it somewhere. This is all done in the main page, this is because, it is the first page the user accesses to enter the chat, so we will have the get the chat name from this page. To do this, create a new asp file and name it default1.asp. Now as we have created the main page lets get the chat name. In order to do this we need to declare a variable so that the user's chat name can be stored somewhere, this is done like this:

<%
Dim chatname

Now we have got a set place to store the user's chat name called 'chatname', but since active server pages utilises server-side scripting, we will have to store this chat name on the user's computer. As ASP uses VBScript and it supports the use of cookies, this can be done easily. To so this we use the 'request.cookies()' function. But wait, we have only declared a variable 'chatname', however we have not stored any information in it so it will be empty. Therefore, where will we find out the user's chat name? Well we will have to collect it from the user. We do this using a html form. As we will be collecting the user's chat name from the form and storing it in a cookie on the user's computer, we will have to ensure that the cookie has no previous data in it. Therefore we use an 'IF/ELSE' structure to check the above condition is true like this:

IF len(Request.Cookies("chatname")) = 0 AND len(Request.Form("chatname")) = 0

THEN

%>

As you can see, a 'THEN' statement is also added here. This is done so that if the cookie named 'chatname' has no value in it, then it can proceed to get a value from the html form like this:

<HTML>
<HEAD>
<TITLE>Welcome to Kumar's Online Chat</TITLE>
</HEAD>

<BODY>
<P>

<CENTER>
Please enter Your Chat Name:
<FORM METHOD="POST" ACTION="default1.asp">
<INPUT NAME="chatname" TYPE="TEXT" SIZE=10>
<INPUT TYPE="SUBMIT" VALUE="ENTER CHATROOM NOW">
</FORM>
</CENTER>

</BODY>
</HTML>

Now as we have collected the user's desired chat name we store it in a cookie using the 'response.cookies()' function on the user's computer. This is done like this:

<%
ELSE

Response.Cookies("Chatname")=Request.Form("chatname")

Now this completes the process of collecting the user's chat name. The next process would be to send a message saying that a new user has entered the chat room. We do this as shown below:

APPLICATION.LOCK
Application("txt13") = Application("txt12")
Application("txt12") = Application("txt11")
Application("txt11") = Application("txt10")
Application("txt10") = Application("txt9")
Application("txt9") = Application("txt8")
Application("txt8") = Application("txt7")
Application("txt7") = Application("txt6")
Application("txt6") = Application("txt5")
Application("txt5") = Application("txt4")
Application("txt4") = Application("txt3")
Application("txt3") = Application("txt2")
Application("txt2") = Application("txt1")
Application("txt1") = "<FONT COLOR=""00FF00"">" & Request.Form("chatname") & " has entered the chat room.</FONT>"
APPLICATION.UNLOCK
%>

Now, this new set of 'application()' functions may confuse you. It is basically creating lines of text messages that will be displayed on the active chat window. As you can see it starts with 1-13. But you may wonder is the '=' is for. Well, we shall leave that out for a minute. Now as you can see on the last line of the application functions (txt1) is assigned a value. This is the message stating that a new user has entered the chat room.

Application("txt1") = "<FONT COLOR=""00FF00"">" & Request.Form("chatname") & " has entered the chat room.</FONT>"

Now then, we shall explore what the = sign is for then. Well, what is happening here is that every time a message or line of text is added it is added only in txt1, so this means that the previous txt1 line will be deleted. However, wait, with the help of the = sign we are saving the txt1 to txt2 and tx2 to txt3 and so on. This way whenever a new message is added, the previous ones up to 13 messages will not be deleted. So, this way many people can chat simultaneously without having their messages deleted. This also gives us another advantage that we can add more lines of messages. There is no limit but too many lines will look messy. I will show you how to add more lines at the end.

The last part of this asp file would be to take the user to the chat window after he has entered the chat name. So we enter the html to do this.

<HTML>
<HEAD><TITLE>Kumar's Online Chat</TITLE>
</HEAD>

<FRAMESET ROWS="80%, 20%" FRAMEBORDER="0" BORDER="false">
<FRAME SRC="display.asp" SCROLLING="auto">
<FRAME SRC="message.asp" SCROLLING="no">
</FRAMESET>

</HTML>
<%
END IF
%>

As you can see above there is an 'END IF' statement to terminate the IF statement earlier in the asp file. This is the end of the first of the four asp files that make up this chat application, which is the main one 'default1.asp'. Now let's look at the last html part of this file. As you can see it is a frames page. The two frames sources are display.asp and message.asp. This is the end of Part 1.