To read the article online, visit http://www.4GuysFromRolla.com/webtech/041000-1.3.shtml

ASP Instant Messaging, Part 3

By Ryan S.


  • Read Part 1
  • Read Part 2

  • In Part 2 we looked at how to check for and send messages on the client. In this part we'll examine how to display users' messages on an ASP page.

    Presented below is the code to display the users messages, which appears in an ASP Nugget in the center frame.

    <html>
    <head>
    <title>Chat center (Not Displayed</title>
    <meta http-equiv="refresh" content="5">
    <!-- This is crucial to ensure
            timely delivery of messages -->
    </head>
    <body bgcolor="white">
    <%
    
      Call CheckMsgs
    
    If not (cChatDB.BOF) Then
       cChatDB.MoveFirst
    End If
    
    If not cChatDB.EOF Then
      strFilter = "(ToID = " & Session("UserID") & " and FromID=" & _
                  Request.QueryString("TalkingTo") & ") or (ToID=" & _
                  Request.QueryString("TalkingTo") & " and FromID=" & _
                  Session("UserID") & ")"
    
      cChatDB.Filter = strFilter
      cChatDB.Sort = "TimeSent DESC"
      If not cChatDB.EOF Then
        response.write "<table">
        if PMIN(Session("Count")) Then
          Session("Count") = 10
        End If
        Count = 0
        Do While not cChatDB.EOF and Count <= cint(Session("Count"))
           response.write "<tr valign=top width=300>"
           response.write "<td valign=top width=50>"
           if lcase(cChatDB("FromName")) = lcase(Session("UserName")) Then
              response.write "<font color=red>" & cChatDB("FromName")
           Else
              response.write "font color=blue>" & cChatDB("FromName")
           End If
           response.write "</font></td>"
           response.write "<td rowspan=2 valign=top width=250>" & _
                          cChatDB("Message") & "</td></tr>"
           response.write "<tr valign=top width=300>"
           response.write " <td valign=top width=50>"
           zH = Hour(cChatDB("TimeSent")
           zM = Minute(cChatDB("TimeSent"))
           zS = Second(cChatDB("TimeSent"))
           If zH > 12 Then
             zH = zH - 12
           End If
           If zM < 10 Then
             zM = "0" & zM
           End If
           If zS < 10 Then
             zS = "0" & zS
           End If
           response.write zH & ":" & zM & ":" & zS
           response.write "</td></tr>"
           count = count + 1
           cChatDB.MoveNext
        Loop
        response.write "</table>"
      End If
    End If
    %>
    
    
    

    And that lil ditty of code takes care of all the display. I opted to stick it into a table, and that way can enforce wrapping. However, as a note, you might want to be careful if someone sticks a whole globule of text in there, such as 80 letter a's without any spaces in there. That can drastically affect the look.

    As for sending messages, it's simply hooking the form together with the ASP, having the form submit into itself, and that way it empties out each time.

    Now, everyone's favorite time, in which I challenge you to come up with your own variants, many of which I myself would like to work on, but have other pressing matters. Possibilities include:

    • Using JavaScript to trigger a sound effect
    • Displaying how long a user is online. You can adapt that to search for a user based on user name, find when they logged on, and do a DateDiff on it.
    • Have OffLine messages, or messages that were in the application variable that the user never received, that they receive the next time they log in
    • Pop up a window from another user if they have messages waiting (I did implement this, through a very crude method. I created a very small recordset (though is anything small in application scope?), and have it simply state a ToID and FromID, and the user checked if their ID was in the ToID field was, and if so, used JavaScript to spawn a chat window with a user with the ToID ID
    • Detect if the user is offline, and if so, make the user aware. I do this also through keeping an array of users online, and then iterating through the array to search for a UserID/UserName pattern, and if it fails, then alert the user using JavaScript popups
    • Group Chat. This would be a case of changing the ToID to be impossible to naturally obtain, such as the inclusion of an "a" prior to it, where the number following the text represents what chat channel. This would be much more of a project, because then you would have to find a method of displaying which users are in the chat room chatting, if the user can have multiple chat rooms, et cetera. It is something I plan to do though, darn it =)
    • Optimize the code to be memory friendly. I know that objects in Application AND Session scope is very UN-memory friendly, as well as makes it hard to scale across multiple servers in a cluster balanced situation. Perhaps some friendly VB programmer would like to write a COM/COM+ component to handle this.

    Anyways, I hope this article has been an interesting read for you. The live version of this chat application will be available at http://www.tupponline.com when I set it live, however, if any of you are curious to see a beta version, http://beta.tupponline.com has it.

    Also, if you were wondering why I fail to close the objects, is because that would ruin the custom nature of them, so please take note of that. You may also which to implement code in the Session_OnEnd and Application_OnEnd that properly takes care of the necessity of cleaning up your variables, such as cChatDB.Close and aChatDB.Close, and then setting them equal to nothing.

    And finally, if any of you programmers had lives before you were sucked into this little "hobby" of programming, and got married and had daughters, then it should be about time for them to be inundated with Teen-Bop. A certain girl quartet was the inspiration for this article, as well as many other fun features, so I'm sure you should have your daughters (or sons, but you know how boys can be =P) check out the group. Their called P.Y.T., signed w/ Epic Records, and have toured w/ Britney Spears & N'Sync last summer. They are about to release their CD in May, and it's no doubt going to be awesome, so it would make a great gift, dads. =) Ok, enough free promotion for "my" girls (I call them that because they are good friends of mine =P)

    God bless,
    Ryan S.

  • Read Part 2
  • Read Read Part 1


  • Article Information
    Article Title: ASP Instant Messaging, Part 3
    Article Author: Ryan S.
    Published Date: Monday, April 10, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/041000-1.3.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers