Click here to Skip to main content
6,596,602 members and growing! (21,728 online)
Email Password   helpLost your password?
Web Development » Ajax and Atlas » Samples     Intermediate

A Sample Chat Application using Mike Schwarz's AJAX Library

By K.sundar

An article on developing a chat application using AJAX and .NET.
C#, Javascript, XML, HTML, Windows, .NET 1.1, ASP.NET, IIS, Ajax, VS.NET2003, Dev
Posted:15 Mar 2006
Views:53,789
Bookmarked:53 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
16 votes for this article.
Popularity: 4.66 Rating: 3.87 out of 5
2 votes, 12.5%
1

2
4 votes, 25.0%
3
1 vote, 6.3%
4
9 votes, 56.3%
5

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


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

Occupation: Web Developer
Location: India India

Other popular Ajax and Atlas articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
GeneralHow to save the chat information PinmemberNaga Raju Vutla19:33 19 Aug '09  
GeneralPrivate Room Pinmemberarpitgold21:18 29 Apr '09  
GeneralDatabase for this project Pinmembermharoon bhatti21:40 11 Dec '08  
GeneralGood! PinmemberJasonC726:26 13 Oct '08  
QuestionChat messenger allows 1-to-1 communication [modified] PinmemberMember 296184522:38 7 Apr '08  
GeneralExcelent sample!! PinmemberZeus>Segundo9:10 29 Aug '07  
Generalhelp needed Pinmembervimala soujanya21:58 21 Jun '07  
GeneralRe: help needed Pinmemberarindam.roy0:52 27 Oct '07  
Questionhow to display text as it is send Pinmemberlata kumari21:18 10 Jun '07  
GeneralNice Pinmember20:30 8 Mar '07  
QuestionQuery Pinmembervcksaran0:04 22 Sep '06  
Questionuser status Pinmemberjasine0:41 8 Aug '06  
Generalsimple and effictive PinmemberRamesh Ramalingam2:00 5 Aug '06  
GeneralAjax enabled DataGrid - Chat Application Example PinmemberOmegaCD3:59 26 May '06  
GeneralCool PinmemberJerok7810:53 12 May '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Mar 2006
Editor: Chris Maunder
Copyright 2006 by K.sundar
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project