Data gets lost in a java script and .Net based chat app

49 views Asked by At

This code has landed on my lap, and I don't have any clue what might be going on. The code is a Java Script in an ascx file as part of a chat system. The problem is that the chat content are lost after about 10 minutes. I don't know if this has anything to do with caching or not. I would like to know what might be causing the loss of chat content in this code.

<script type="text/javascript">
    var roomid = '<%= ChatRoomID %>';
    var status = "";

    var msgTimer = "";
    function StartTimers(sec) {
        msgTimer = window.setInterval("DoRefresh()", sec);
    }
    function StopTimers() {
        window.clearInterval(msgTimer);
        msgTimer = "";
    }
     function SendMsg() {
            if (!xmlrequest)
                InitObject();

            if (xmlrequest) {
                var obj = document.getElementById("txtMsg");
                if (obj != null && obj.value.trim().length > 0) {
                    StopTimers();
                    status = "completed";
                    var nowTime = new Date().getTime(); //Get now time as random
                    var params = "act=sendmsg&rid=" + roomid + "&gid=&msg=" + encodeURIComponent(obj.value) + "&st=" + status;
                    //xmlrequest.abort();
                    xmlrequest.onreadystatechange = SendMsg_Callback;   //(GetBrowserVer() != "Firefox") ? (SendMsg_Callback) : (SendMsg_Callback());   
                    xmlrequest.open("POST", "/site/function/chat.ashx?t=" + nowTime, false);
                    xmlrequest.setRequestHeader("If-Modified-Since", "0");  // Avoid the cache in IE
                    xmlrequest.setRequestHeader("Cache-Control", "no-cache");
                    xmlrequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                    xmlrequest.setRequestHeader("Content-length", params.length);
                    xmlrequest.setRequestHeader("Connection", "close");
                    xmlrequest.send(params);
                    //xmlrequest.onreadystatechange = SendMsg_Callback;   //(GetBrowserVer() != "Firefox") ? (SendMsg_Callback) : (SendMsg_Callback());   
                    obj.value = "";
                    CharacterCount(obj);
                    obj.focus();
                    StartTimers(60);
                }
            }
        }

        function SendMsg_Callback() {
            if (xmlrequest.readyState == 4 && (xmlrequest.status == 200 || xmlrequest.status == 0)) {
                // sometimes, the completed status will equal zero in Firefox
            }
        }

    function DoRefresh() {
        StopTimers();
        Refresh();
    }

    function Refresh() {
        if (!xmlrequest)
            InitObject();

        if (xmlrequest) {
            var nowTime = new Date().getTime(); //Get now time as random
            var params = "act=refresh&rid=" + roomid + "&gid=&st=" + status;
            //xmlrequest.abort();
            xmlrequest.onreadystatechange = Refresh_Callback; //(GetBrowserVer() != "Firefox") ? (Refresh_Callback) : (Refresh_Callback());
            xmlrequest.open("POST", "/site/function/chat.ashx?t=" + nowTime, true);
            xmlrequest.setRequestHeader("If-Modified-Since", "0");  // Avoid the cache in IE
            xmlrequest.setRequestHeader("Cache-Control", "no-cache");
            xmlrequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlrequest.setRequestHeader("Content-length", params.length);
            //xmlrequest.setRequestHeader("Connection", "close");
            xmlrequest.send(params);
            xmlrequest.onreadystatechange = Refresh_Callback; //(GetBrowserVer() != "Firefox") ? (Refresh_Callback) : (Refresh_Callback());
        }
    }

    function Refresh_Callback() {
        if (xmlrequest.readyState == 4 && (xmlrequest.status == 200 || xmlrequest.status==0)) {
        //if (xmlrequest.readyState == 4 && xmlrequest.status == 200) {
            // Is ready
            var response = xmlrequest.responseText;

            //alert(response);
            //document.write("<p>" + response + "</p>");

            if (response != undefined && response.length > 0) {
                var vals = null; 
                vals = response.split("<<<<<>>>>>");
                if (vals == null || vals.length < 2) {
                    document.getElementById("msglist").value = response;
                    return;
                }
                /// The part one of response value is user list
                var obj = document.getElementById("userlist");
                if (obj != null && vals[0].length > 0) {
                    //alert(vals[0]);
                    obj.options.length = 0;
                    var options = vals[0].split(",");
                    for (i = 0; i < options.length; i++) {
                        obj.options.add(new Option(options[i], options[i]));
                    }
                }
                /// The part two of response value is chat message list
                obj = document.getElementById("msglist");
                if (obj != null && vals[1].length > 0) {
                    obj.innerHTML += vals[1] + "<br/>";
                    //obj.scrollIntoView("true");
                    obj.scrollTop = obj.scrollHeight;
                }

                /// The part three of response value is command or status
                if (vals.length > 2) {
                    if (vals[2].toLowerCase() == "reload") {
                        //var sUrl = window.location.href;
                        //sUrl += (sUrl.indexOf("?") > 0) ? "&" : "?" + "r=t";
                        //window.location.href = sUrl;  // window.location.href;
                        redirectURL(window.location.href);
                    }
                    else {
                        obj = document.getElementById("divStatus");
                        if (obj != null) {
                            obj.innerHTML = vals[2];
                        }
                    }
                }
            }
            StartTimers(2500);
        }
    }
//////////////////////// End Call Ajax Function //////////////////////////////

function GetBrowserVer() {
    var OsObject = "";
    if (navigator.userAgent.toLowerCase().indexOf("msie") > 0) {
        return "MSIE";       //IE
    }
    if (isFirefox = navigator.userAgent.toLowerCase().indexOf("firefox") > 0) {
        return "Firefox";     //Firefox
    }
    if (isSafari = navigator.userAgent.toLowerCase().indexOf("safari") > 0) {
        return "Safari";      //Safan
    }
    if (isCamino = navigator.userAgent.toLowerCase().indexOf("camino") > 0) {
        return "Camino";   //Camino
    }
    if (isMozilla = navigator.userAgent.toLowerCase().indexOf("gecko") > 0) {
        return "Gecko";    //Gecko
    }
}

function getKeycode(evt) {
    // Usually evt is indicated as keypress or other key events
    var keycode = 0;
    if (evt != undefined && evt != null && "which" in evt) {// NN4 & FF & Opera    
        keycode = evt.which;
    }
    else if (evt != undefined && evt != null && "keyCode" in evt) {// Safari & IE4+
        keycode = evt.keyCode;
    }
    else if ("keyCode" in window.event) {// IE4+
        keycode = window.event.keyCode;
    }
    else if ("which" in window.event) {
        keycode = evt.which;
    }
    return keycode;
}

function DoSendMsg(e) {
    //debugger
    if (e.ctrlKey && getKeycode(e) == 13) {
        SendMsg();
    }
}

String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function redirectURL(url) {
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
        var referLink = document.createElement('a');
        referLink.href = url;
        document.body.appendChild(referLink);
        referLink.click();
    } else {
        window.location.href = url;
    }
}

var CurrentUrl = '<%= Request.Url.ToString().ToLower() %>';
window.onload = function() {
    if (CurrentUrl.indexOf("config_page_edit.aspx") < 0 && CurrentUrl.indexOf("config_page_preview.aspx") < 0) {
        StartTimers(500);
    }
}
0

There are 0 answers