Uzm Posted July 31, 2008 Share Posted July 31, 2008 Hello guys. I've been working on an AJAX/PHP/MySQL-chat for a bit now, and recently run into a problem that i can't really solve, and getting a bit desperate. The problem is that i'm getting loads of "undefined" errors, and sometimes requests (aspecially on function getData()) ain't getting response. I've been googling about this a lot, read loads of topics on loads of forums, and more, but i can't solve my problem. Could you help me a bit? var chatUrl = "http://localhost/chat/"; var imgloaded = new Array(); var msgtime = 1; var imgdir = "http://localhost/chat/images/"; var sysimages = new Array("info.gif", "pr.gif", "privat.gif"); var sysloaded = new Array(); var loggedIn = false; var userId; var userName; var level; var yo; var exitRequest = createRequest(); var sendMsgRequest = createRequest(); var getDataRequest = createRequest(); var enterChatRequest = createRequest(); var loadedTimeout = 3500; var loadingTimeout = 3500; var date = new Date(); var timestamp = date.getTime(); var images = new Array(["01.gif", ":цоол"],["02.gif", ":слёзы"],["03.gif", ":дурак"],["04.gif", ":цветы"],["05.gif", ":хурхур"], ["06.gif", ":неа"],["07.gif", ":о"],["08.gif", ":ржу"],["09.gif", ":глазки"],["10.gif", ":роза"],["11.gif", ""],["12.gif", ":сплю"],["13.gif", ""],["14.gif", ":вылез"],["15.gif", ":растяг"],["16.gif", ":ога"],["17.gif", ""],["18.gif", ":обида"],["19.gif", ":двигай"],["20.gif", ":правила"],["21.gif", ":гнев"],["22.gif", ":гы"],["23.gif", ":ура"],["24.gif", ":хмм"],["25.gif", ":ангел"],["26.gif", ":стоп"],["27.gif", ":подлиз"],["28.gif", ":смех"],["29.gif", ":любовь"],["30.gif", ":пока"],["31.gif", ":прости"],["32.gif", ":ляля"],["33.gif", ":смущение"],["34.gif", ":язык"],["35.gif", ":поцелуй"],["36.gif", ":целуются"],["37.gif", ":айкидо"],["38.gif", ":отдых"],["39.gif", ":босс"],["40.gif", ":команда"],["41.gif", ":псих"],["42.gif", ":ковыряю"],["43.gif", ":ах"],["44.gif", ":кик"],["45.gif", ":автомат"],["46.png", ")"],["47.gif", ":палец"],["48.png", ":нини"],["49.png", ":аццтой"],["50.png", ":зелёный"],["51.png", ":молчу"],["52.png", ":молодец"],["53.png", ":гасп"],["54.png", ":херасе"],["55.png", ":зубы"],["56.png", ":брр"],["57.gif", ":слушаю"],["58.gif", ":помидоры"],["59.gif", ":крошим"],["60.gif", ":стена"],["61.gif", ":мат"],["62.gif", ":курю"],["63.gif", ":кенни"],["64.gif", ":программер"],["65.png", ":слюни"],["66.gif", ":соска"],["67.gif", ":пиво"]); for (i = 0; i < sysimages.length; i++) { sysloaded[i] = new Image(); sysloaded[i].src = imgdir + sysimages[i]; } function createRequest() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else if(window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); } else { alert("Error initializing XMLHttpRequest!"); } } function loaded () { document.getElementById("win").style.visibility = "visible"; document.getElementById("main").style.visibility = "hidden"; yo = setTimeout('getData()', loadedTimeout); } function exit () { exitRequest.open("POST", chatUrl + "exit.php", true); exitRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); exitRequest.send(""); clearTimeout(yo); loggedIn = false; document.getElementById("main").style.visibility = "visible"; document.getElementById("win").style.visibility = "hidden"; } function send (msg, to) { if (sendMsgRequest.readyState == 4 || sendMsgRequest.readyState == 0) { sendMsgRequest.open("POST", chatUrl + "send.php", true); sendMsgRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); sendMsgRequest.send("msg=" + encodeURIComponent(msg) + "&userId=" + userId + "&to=" + to + "×tamp=" + timestamp); } } function getData() { if (getDataRequest.readyState == 4 || getDataRequest.readyState == 0) { getDataRequest.open("POST", chatUrl + "get.php", true); getDataRequest.onreadystatechange = getDataEvent; getDataRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); getDataRequest.send("userId=" + userId + "&msgtime=" + msgtime + "×tamp=" + timestamp); } if (loggedIn == true) { yo = setTimeout('getData()', loadingTimeout); } } function getDataEvent() { if (getDataRequest.readyState == 4) { if (getDataRequest.status == 200) { if (getDataRequest.responseText.substr(0,1) == "|") { clearTimeout(yo); alert(getDataRequest.responseText.substr(1,getDataRequest.responseText.length - 1)); exit(); } else { var lines = getDataRequest.responseText.split('\n'); lines.length = lines.length - 1; if (lines.length > 0) { onlineUserList = lines[0].split("|"); onlineUserList.length = onlineUserList.length - 1; } if (lines.length > 1) { for (i = 1; i < lines.length; i++) { var msg = lines[i].split("|"); try { parent.talk.printmsg(msg[0],msg[1],msg[2],msg[3]); } catch (e) { } msgtime = msg[4]; } } seichas = document.getElementById("chatlan"); mysp = document.createElement("span"); hrprfr = document.createTextNode("ЮЗЕРОВ ОНЛАЙН: " + onlineUserList.length); mysp.appendChild(hrprfr); seichas.replaceChild(hrprfr, seichas.firstChild); parent.menu.printmenu(); } } else { alert("Ошибка запроса к серверу: " + enterRequest.status); } } } function enterChat (userName, password) { if (enterChatRequest.readyState == 4 || enterChatRequest.readyState == 0) { enterChatRequest.open("POST", chatUrl + "enter.php", true); enterChatRequest.onreadystatechange = enterChatEvent; enterChatRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); enterChatRequest.send("login=" + userName + "&password=" + password); } } function enterChatEvent() { if (enterChatRequest.readyState == 4) { if (enterChatRequest.status == 200) { if (enterChatRequest.responseText.indexOf("|") != -1) { var user = enterChatRequest.responseText.split("|"); userId = user[0]; userName = user[1]; level = user[2]; loggedIn = true; loaded(); } else if (enterChatRequest.responseText == 'n') { alert("Ошибка при входе в чат: Вы ввели неправильный логин и/или пароль!"); } else { alert("Ошибка при входе в чат: " + enterChatRequest.responseText); } } else { alert("Ошибка запроса к серверу: " + enterChatRequest.status); } } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.