Click here to Skip to main content
15,892,768 members
Articles / Web Development / ASP.NET

Chat Application in ASP.NET Using AJAX (Pop-up Windows)

Rate me:
Please Sign up or sign in to vote.
4.83/5 (28 votes)
7 May 2010CPOL3 min read 151.9K   23.2K   66  
Chat application in ASP.NET using AJAX and SQL Server.
var chats = new Array();
var top = 100;
var left = 100;
function OpenChatBox(uid, chatid) {
    var win = window.open("ChatBox.aspx?uid=" + uid + "&cid=" + chatid, "ChatWindow" + chatid + document.getElementById('hidCurrentUser').value, "status=0,toolbar=0, menubar=0, width=450, height=550");
    top = top + 50;
    if (top > screen.availHeight - 550)
        top = 100;
    left = left + 50;
    if (left > screen.availWidth - 450)
        left = 100;
    
    win.moveTo(left, top);
    chats[chats.length] = win;
    return false;
}

function closeChats() {
    for (iind = 0; iind < chats.length; iind++) {
            try
            {
                chats[iind].close();
            }
            catch(e){}
        }  
}

function PingServer() {
    PingServerForMsg()
    setTimeout(PingServer, 5000);
}

var xmlHttp;
var requestURL = 'GetChatMsg.aspx';
var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0;
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5") != -1) ? 1 : 0;
var is_opera = ((navigator.userAgent.indexOf("Opera6") != -1) || (navigator.userAgent.indexOf("Opera/6") != -1)) ? 1 : 0;
//netscape, safari, mozilla behave the same??? 
var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0;

function PingServerForMsg() {
       //Append the name to search for to the requestURL 
        var url = requestURL;

        //Create the xmlHttp object to use in the request 
        //stateChangeHandler will fire when the state has changed, i.e. data is received back 
        // This is non-blocking (asynchronous) 
        xmlHttp = GetXmlHttpObject(stateChangeHandler);

        //Send the xmlHttp get to the specified url 
        xmlHttp_Get(xmlHttp, url);   
}

//stateChangeHandler will fire when the state has changed, i.e. data is received back 
// This is non-blocking (asynchronous) 
function stateChangeHandler() {
    //readyState of 4 or 'complete' represents that data has been returned 
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete') {
        //Gather the results from the callback 
        var str = xmlHttp.responseText;

        if (str != "" ) {
            //document.getElementById('txtMsg').value = str;
            //eval(str);
            var msgs = str.split('#');

            for (ind = 0; ind < msgs.length; ind++) {
                msgs[ind] = msgs[ind].replace("_@HASH__", "#");
                var msg = msgs[ind].split("&");
                msg[0] = msg[0].replace("_@AMP__", "&");
                msg[1] = msg[1].replace("_@AMP__", "&");
                msg[2] = msg[2].replace("_@AMP__", "&");            
                var blnExisting = false;
                for (iind = 0; iind < chats.length; iind++) {
                    try
                    {
                        if (chats[iind] != null && chats[iind].name == "ChatWindow" + msg[1] + document.getElementById('hidCurrentUser').value)
                        blnExisting = true;
                    }
                    catch(e){}
                }  
                if( blnExisting == false)
                    OpenChatBox(msg[0], msg[1]);
            }
        }
    }
}

// XMLHttp send GET request 
function xmlHttp_Get(xmlhttp, url) {
    xmlhttp.open('GET', url, true);
    xmlhttp.send(null);
}

function GetXmlHttpObject(handler) {
    var objXmlHttp = null;    //Holds the local xmlHTTP object instance 

    //Depending on the browser, try to create the xmlHttp object 
    if (is_ie) {
        //The object to create depends on version of IE 
        //If it isn't ie5, then default to the Msxml2.XMLHTTP object 
        var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP';

        //Attempt to create the object 
        try {
            objXmlHttp = new ActiveXObject(strObjName);
            objXmlHttp.onreadystatechange = handler;
        }
        catch (e) {
            //Object creation errored 
            alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled');
            return;
        }
    }
    else if (is_opera) {
        //Opera has some issues with xmlHttp object functionality 
        alert('Opera detected. The page may not behave as expected.');
        return;
    }
    else {
        // Mozilla | Netscape | Safari 
        objXmlHttp = new XMLHttpRequest();
        objXmlHttp.onload = handler;
        objXmlHttp.onerror = handler;
    }

    //Return the instantiated object 
    return objXmlHttp;
} 

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
Hi, I am Narsimlu Keshagouni working as Technical Lead in Hyderabad, India.

Comments and Discussions