Click here to Skip to main content
Click here to Skip to main content

A Sample Chat Application using Mike Schwarz's AJAX Library

By , 15 Mar 2006
 

Sample Image

Introduction

This article describes a sample chat application using Mike Schwarz's AJAX library. With the development of AJAX as a new development mode for the Web, every one seems to be more interested in it. Here, I have used an AJAX library for .NET developed by Mike Schwarz. This library is basically an HTTP handler which redirects AJAX requests from a client to the appropriate types. This library in turn uses JSON - JavaScript Object Notation which is a data-interchange format; using JSON, we can access even complex types on the client side.

Using the code

To use Schwarz's AJAX library, we have to register the type which we want to associate for AJAX calls:

// Register
Ajax.Utility.RegisterTypeForAjax(typeof(Chat));

Add an HTTP handler section to web.config:

<httpHandlers>
    <add verb="POST,GET" path="ajax/*.ashx" 
         type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>

You can find further details in the library documentation. Now, to start out with the code: the application mainly consists of two files, Chat.aspx and DragAndPost.js. The DragAndPost.js consists of a function which posts the AJAX request and gets a hashtable back and populates the open chat rooms.

You will have a context menu to open private chat rooms to which you can use the same context to invite as well. The source code is pretty simple, all we need to do is configure a lot of things in JavaScript, like opening divs on double click etc. Post your comments, so that I can improve it.

This function creates a div on the double click of a chat room:

function CreateDiv(DivID,Roomid)
{
    ParentElement=document.getElementById("TRCHAT");
    var Divelement=document.createElement("DIV");
    Divelement.align="right";
    Divelement.setAttribute('id',DivID);
    Divelement.setAttribute('name',DivID);
    Divelement.style.width=250;
    Divelement.style.height=300;
    Divelement.style.left=200;
    Divelement.style.top=170;
    Divelement.style.position='absolute';
    Divelement.style.background='#9fc9d3';
    Divelement.style.cursor='Move';
    ParentElement.appendChild(Divelement);
    /
    /
}
//The Drag part of Div is handled By
function movemouse(e)
{
  if (isdrag)
  {
    fobj.style.left = nn6 ? tx + e.clientX - x : 
                            tx + event.clientX - x;
    fobj.style.top  = nn6 ? ty + e.clientY - y : 
                            ty + event.clientY - y;
    return false;
  }
}
function selectmouse(e) 
{
  fobj = nn6 ? e.target : event.srcElement;
  if (fobj.parentElement.tagName!="DIV" 
                 && fobj.tagName=="DIV")
  {
        isdrag = true;
        tx = parseInt(fobj.style.left+0);
        ty = parseInt(fobj.style.top+0);
        x = nn6 ? e.clientX : event.clientX;
        y = nn6 ? e.clientY : event.clientY;
        document.onmousemove=movemouse;
        return false;
  }
}
document.onmousedown=selectmouse;
document.onmouseup=new Function("isdrag=false");

I have used a string builder like class to speed up the client side string manipulation:

//StringBufferLikeClase
function StringBuffer() { 
  this.buffer = [];
} 

StringBuffer.prototype.append = 
  function append(string) { 
    this.buffer.push(string); 
    return this; 
}; 

StringBuffer.prototype.toString = 
  function toString() { 
    return this.buffer.join(""); 
};

History

  • Posted on March 07, 2006.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

K.sundar
Web Developer
India India
Member
Hi,
i had been programming in .NET for 2 years,
Visit me at my Space
http://spaces.msn.com/ksundar

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalnice but needs some enhancementmemberqusai8422 Apr '11 - 2:24 
it seems nice but doesn't work fine with firefox 4
 

thanks
GeneralMy vote of 4memberqusai8422 Apr '11 - 2:22 
it seems nice and useful but I tried it in firefox 4 doesn't work fine Smile | :)
QuestionHow to save the chat informationmemberNaga Raju Vutla19 Aug '09 - 18:33 
Hi,
 
This is very nice. But here one problem came. I need to save the chat information. Can you plz help me how to save the chat information
GeneralDatabase for this projectmembermharoon bhatti11 Dec '08 - 20:40 
Where I can download the database for this project? as I couldn't find that on code project.
GeneralGood!memberJasonC7213 Oct '08 - 5:26 
Smile | :)
QuestionChat messenger allows 1-to-1 communication [modified]memberMember 29618457 Apr '08 - 21:38 
How send chat messenger allows 1-to-1 communication between users and also send file or image file.
 
modified on Tuesday, April 8, 2008 3:48 AM

GeneralExcelent sample!! [modified]memberZeus>Segundo29 Aug '07 - 8:10 
it works as a dream !!
modified on Monday, May 9, 2011 3:30 PM

Generalhelp neededmembervimala soujanya21 Jun '07 - 20:58 
ok ...the chat application is very cool and very user friendly.
I have done a slight modification to your chat application such that when we click on the user name ,the pop window opens and it enables us to chat with that particular user in a separate roo.But ,iam facing difficulty in chatting with the particular user.Whenever i click on a particular user ,the pop-up window opens but when i type any message in that window it is displaying null .Can you suggest me to overcome this problem.
 
Soujanya
GeneralRe: help neededmemberarindam.roy26 Oct '07 - 23:52 
hi...
 
the same problem is with my code too..
 
could u help me if u resolved ur problem...
 
do mail at royari@gmail.com
 
cell no:9823509334
Questionhow to display text as it is sendmemberlata kumari10 Jun '07 - 20:18 
hi all
when i copy two thre line code how to display in div as it is pasted in texbox in diffrent line
not in same line
messagecell1.innerHTML=messageTosend;
in my case messageTosend it contains message n i want to display in onmessagecell1 but it take in one line not in format i enter
 

 
lata

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 15 Mar 2006
Article Copyright 2006 by K.sundar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid