I've noticed a few people request to do a chat app from ASP so I've knocked one up. I see you've already got a chat ASP script (from FailSafe), Anyway, feel free to add this code straight to your site, it's not that feature rich but it demonstrates the kind of things you need to do. It does not require a login but instead keeps a track of active users based on all the people who have posted within a certain timeframe. There are two options you can configure, how many minutes to keep active users and how many posts to be shown on screen. The main chat area shows you who posted what, at what time and what IP address they posted from, and it also shows a list of active users. It's nice and simple and includes the code to create the database and I've even commented the page that does all the work (although documentation is not my strong point!) Menu.asp <%@ Language=VBScript %> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> </HEAD> <BODY> <!-- Author: Adrian Forbes -->
<p> <a href="createdb.asp">Create the database</a><br> <a href="config.asp">Configure the parameters</a><br> <a href="chat.asp">Chat</a><br> </p>
</BODY> </HTML>
|
CreateDB.ASP <%@ Language=VBScript %> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> </HEAD> <BODY> <!-- Author: Adrian Forbes -->
<% sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath ("/examples/test.mdb") & ";" set objRS = CreateObject("ADODB.Recordset") sSQL = "CREATE TABLE chat (ID int identity, strText memo not null, strUser varchar(50) not null" & _ sSQL = " strIP varchar(15) not null, dtDatePosted datetime not null)" objRS.Open sSQL, sConnect
sSQL = "CREATE TABLE chatuser (strUser varchar(50) not null, dtLastPosted datetime not null)" objRS.Open sSQL, sConnect
set objRS.ActiveConnection = nothing set objRS = nothing
Application.Lock Application("MaxMessages") = 10 Application("ActiveTime") = 5 Application.UnLock
%>
<p>Database created.</p> <p><a href="menu.asp">Back to menu</a></p> </BODY> </HTML>
|
Config.asp <%@ Language=VBScript %> <% sMax = trim(Request.Form("txtMax")) sActive = trim(Request.Form("txtActive"))
if isnumeric(sMax) then Application.Lock Application("MaxMessages") = Clng(sMax) Application.UnLock end if
if isnumeric(sActive) then Application.Lock Application("ActiveTime") = Clng(sActive) Application.UnLock end if %> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> </HEAD> <BODY> <!-- Author: Adrian Forbes -->
<p> <form action="config.asp" method=post> <table border=0> <tr> <td>Max number of messages</td><td><input type=text name=txtMax value="<%=Application("MaxMessages")%>"></td></tr> <td>Time to keep users active (mins)</td><td><input type=text name=txtActive value="<%=Application("ActiveTime")%>"></td></tr> </table> <p><input type=submit value="Save"></p> </form> </p> <p><a href="menu.asp">Back to menu</a></p> </BODY> </HTML>
|
Chat <%@ Language=VBScript %> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> </HEAD> <BODY> <!-- Author: Adrian Forbes -->
<p> <form action="storechat.asp" method=post> You Name:<input type=text name=txtName value="<%=Session("UserName")%>" maxlength=50><br> <textarea name=txtChat cols=50 rows=3></textarea> <br> <input type=submit value="Chat"> </form> </p> <p> Active users: <select size=1> <% ' Select the list of users from the ChatUsers table set objRS = CreateObject("ADODB.Recordset") objRS.Open "SELECT strUser FROM chatuser ORDER BY strUser", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath ("/examples/test.mdb") & ";" while not objRS.EOF Response.Write "<option>" & objRS("strUser") & vbCRLF objRS.MoveNext wend objRS.Close %> </select> </p>
<p> <% ' Select each row form the Chat table objRS.Open "SELECT strUser, dtDatePosted, strIP, strText FROM chat ORDER BY dtDatePosted DESC" while not objRS.EOF Response.Write "<p><b>" & objRS("strUser") & "</b> (at <b>" & FormatDateTime(objRS("dtDatePosted"), 3) & _ "</b> from IP <b>" & objRS("strIP") & "</b>) said<br>" & vbCRLF Response.Write replace(server.HTMLEncode(objRS("strText")), vbCRLF, "<br>") & "</p>" & vbCRLF objRS.MoveNext wend objRS.Close set objRS.ActiveConnection = nothing set objRS = nothing %> </p> </BODY> </HTML>
|
StoreChat.asp <%@ Language=VBScript %> <% ' Author: Adrian Forbes
' This script performs a number of tasks ' 1 Add text to the chat table in the database ' 2 Maintain the list of active users ' 2.1 Check if user is already active (i.e. do they exist in the ChatUsers table) ' 2.2 If they are not active (i.e. a new user) then insert their details into the table ' 2.3 If they are active update their record to show that they have just posted ' 3 Delete excess messages to ensure that only the configured amount are in the chat table
' Get the selected usermae sUser = trim(Request.Form("txtName"))
' Store it in the session Session ("UserName") = sUser
' Get the chat text sText = trim(Request.Form("txtChat"))
' if either the username or chat text are blank then redirect back to chat page if len(sUser) = 0 or len(sText) = 0 then Response.Redirect "chat.asp" end if
' set up the constants we are using adCmdText = 1 adVarChar = 200 adDBTimeStamp = 135 adParamInput = 1
' Open our connection, this will be reused throughout
set objConnect = CreateObject("ADODB.Connection") objConnect.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &_ Server.MapPath ("/examples/test.mdb") & ";" objConnect.Open
' 1 First of all insert our chat text into the database
set objCommand = CreateObject("ADODB.Command") with objCommand set .ActiveConnection = objConnect .CommandType = adCmdText .CommandText = "insert into chat (strText, strUser, strIP, dtDatePosted) values (?, ?, ?, Now())" .Parameters.Append .CreateParameter ("strText", adVarChar, adParamInput, len(sText), sText) .Parameters.Append .CreateParameter ("strUser", adVarChar, adParamInput, 50, sUser) .Parameters.Append .CreateParameter ("strIP", adVarChar, adParamInput, 15, Request.ServerVariables("REMOTE_ADDR")) .Execute end with set objCommand.ActiveConnection = nothing set objCommand = nothing
' 2.1 Now we have to find out if this user exists in the chatusers table, i.e. are they ' already active?
set objCommand = CreateObject("ADODB.Command") with objCommand set .ActiveConnection = objConnect .CommandType = adCmdText ' This will return how many times they exist in the table. 0 means they are new ' 1 means they already exist. A user can't exist more than once in this table .CommandText = "select count(*) from ChatUser where strUser = '" & sUser & "'" set objRS = .Execute if objRS(0) = 0 then ' 2.2 count is 0 so we have to add this user to the table .CommandText = "insert into ChatUser (strUser, dtLastPosted) values (?, Now())" else ' 2.3 count is not 0 so update this users entry to show that they have just posted .CommandText = "update ChatUser set dtLastPosted = Now() where strUser = ?" end if .Parameters.Append .CreateParameter ("strUser", adVarChar, adParamInput, 50, sUser) .Execute end with set objCommand.ActiveConnection = nothing set objCommand = nothing
' Now we want to delete all users in the ChatUser table who have not posted within the ' configured time limit
set objCommand = CreateObject("ADODB.Command") with objCommand set .ActiveConnection = objConnect .CommandType = adCmdText ' Use the DateDiff function to delete where the difference between now and when they last ' posted is > Application("ActiveTime") minutes .CommandText = "delete from ChatUser where DateDiff (""n"", dtLastPosted, Now()) > " & Application("ActiveTime") .Execute end with set objCommand.ActiveConnection = nothing set objCommand = nothing
' 3 Now we want to delete the excess posts
set objRS = CreateObject("ADODB.Recordset") ' Get a list of all posts in descending order objRS.Open "SELECT ID FROM chat ORDER BY dtDatePosted DESC", objConnect
lID = 0 i = 0 bExit = false ' Loop through each post while not objRS.EOF and not bExit i = i + 1 if i = Clng(Application("MaxMessages")) then ' The number of posts is bigger than the cofigured limit of Application("MaxMessages") ' Get the ID of this record as we want to delete all others lID = objRS("ID") bExit = true end if objRS.MoveNext wend
objRS.Close
' Check if lID is > 0, if it is then there were more than the configured limit of ' messages if lID > 0 then ' Delete all messages whose ID is less than this ID as they are too far down the list objRS.Open "DELETE FROM chat WHERE ID < " & lID end if set objRS.ActiveConnection = nothing set objRS = nothing
' Close our connection objConnect.Close set objConnect = nothing
Response.Redirect "chat.asp" %>
|
|
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP Code Articles More By Adrian Forbes developerWorks - FREE Tools! | Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo. FREE! Go There Now!
| | | | Join us for this on demand webcast to learn about developing complex systems more quickly and efficiently. We'll cover market drivers for developing, governing and reusing systems software assets and how you can develop system software assets with Rational Asset Manager. FREE! Go There Now!
| | | | WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
| | | | Download a free trial version of IBM DB2 9.5 for Linux, UNIX, and Windows. DB2 9 is the result of a five-year development project that transformed traditional (static) database technology into an interactive data server that merges the high performance and ease of use of DB2 with the self-describing benefits of XML. FREE! Go There Now!
| | | | Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
| | | | Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered! FREE! Go There Now!
| | | | Because access to government information continues to be an area of concern for many U.S. citizens with disabilities, the U.S. government enacted Section 508 of the Rehabilitation Act in 2001 to ensure that government agencies create accessible Web content, enabling all citizens to access the information they need. A fully accessible Web site makes Web content accessible to all individuals, including those with disabilities, who may be accessing Web content via a variety of user agents. Common user agents include standard Web browsers, text-only browsers, assistive devices and mobile devices such as cell phones or personal digital assistants (PDAs). FREE! Go There Now!
| | | | Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, where he will overview Rational’s new offerings and programs to help customers accelerate software innovation on System z. He will discuss how these solutions help organizations extend their core business processes toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
| | | | Join the IBM Watchfire team for an informative discussion on techniques and best practices to proactively manage Web application security and how to effectively build application security testing into the software development lifecycle (SDLC). In this Software Delivery Platform webcast you will learn: How to better understand potential web application security vulnerabilities, best practices and how to effectively integrate application security testing into the software development lifecycle, the importance of detecting and removing software vulnerabilities during application development. FREE! Go There Now!
| | | | The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |