Jump to content

EchoFool

Members
  • Posts

    1,074
  • Joined

  • Last visited

Everything posted by EchoFool

  1. Do i put it in the while loop or after the json encode?
  2. It does change if a user posts a message. Plus the point of COMET JS is to use server push so the script keeps running http://www.zeitoun.net/articles/comet_and_php/start find with ctrl+ f : "The Backend Script" You will see they add an infinite loop on purpose cos its the only way to do Comet JS
  3. Yeah i though that but i got this from a tutorial and theres doesn't do it =/ Which is why im confused
  4. Hey i have a script using COMET server push effect, but it causes my browser to hang and i cannot understand why. Heres the script that deals with PHP: <?php session_name('Current_User'); session_set_cookie_params(0, '/', '.mydomain.com'); // i use a subdomain so this is needed session_start(); include("connect.php"); function getNewMessagesSince($timestamp) { $Get = mysql_query("SELECT Count(push) AS Total FROM messages WHERE LastUpdate='$timestamp'") or die(mysql_error()); $row = mysql_fetch_assoc($Get); if($row['Total'] != 1) { $Get = mysql_query("UPDATE push SET LastUpdate='".time()."'") or die(mysql_error()); $Get = mysql_query("SELECT Message FROM messages WHERE Deleted='0' ORDER BY postedon DESC LIMIT 1") or die(mysql_error()); $row = mysql_fetch_assoc($Get); return array( 'lastupdate' => time(), 'html' => $row['Message'].' <br/>' ); } return false; } while(! ($row = getNewMessagesSince($_POST['lastupdate']))) { //every half a second usleep(50000); } echo json_encode($row); ?> Any ideas on what i got wrong ?
  5. Okay here is the connection script: function ajaxPost(url, postData, callback) { var req; try { req = window.XMLHttpRequest ? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { // browser does not have ajax support } req.onreadystatechange = typeof callback == 'function' ? callback : function() { if (req.readyState == 4 && req.status == 200) { if(typeof callback == 'string') callback = document.getElementById(callback); if(callback) callback.innerHTML = req.responseText; } }; req.open('POST', url, true); req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); req.send(postData); return false; } And the script you have already seen: function Chat(div, url) { this.div = div; this.url = url; this.start = new Date().getTime(); this.start = function(lastupdate) { lastupdate = lastupdate ? lastupdate : new Date().getTime() ajaxPost(this.url, 'lastupdate='+ lastupdate, function(up) { return function() { if (this.readyState == 4) { // success if(this.status == 200) { var data = eval('(' + this.responseText + ')'); // start next timer up.start(data.lastupdate); // fill chat var original = document.getElementById(up.div).innerHTML; var index = original.indexOf('<br>') + 4; var partial = original.substr(index); alert(partial); document.getElementById(up.div).innerHTML = partial + data.html; //document.getElementById(up.div).innerHTML += data.html; //alert(document.getElementById(up.div).innerHTML); //alert(document.getElementById(div).innerHTML); } // connection error else { // try again in .8 of a seconds setTimeout(function(){ up.start(lastupdate); }, 800); } } }; }(this)); }; } var chat = new Chat('chat', 'backend.php'); Backend.php: function getNewMessagesSince($timestamp) { //get newest line (all of this stuff works so just removed it to keep it simple return array( 'lastupdate' => time(), 'html' => $row['Message'].' <br/>' ); } while(! ($row = getNewMessagesSince($_POST['lastupdate']))) { //every half a second usleep(50000); }
  6. Sure thing, sadly as its new years eve i have no time today so check back tomorrow and ill have posted the code up okay ? Thanks for your time helping and hope you have a fun new years night!
  7. Yes partial is blank. Is this why its not working ? The data for data.html comes from a PHP function on a seperate script which is collected to this JS script.
  8. It only shows 1 line - so when a new line comes in from a user only the one line shows no idea where the rest went. This is what i got: var data = eval('(' + this.responseText + ')'); // start next timer up.start(data.lastupdate); // fill chat var original = document.getElementById(up.div).innerHTML; var index = original.indexOf('<br>') + 4; var partial = original.substr(index); document.getElementById(up.div).innerHTML = partial + data.html;
  9. Okay its sorta improved but its not cutting the string properly. Also the <br/> no longer seems to be in the divs so nothing is going to new lines aka its just a big block of text. Edit: Also it doesn't limit it to the number of lines that is put in the JS either
  10. I do not see how - if you look my second post step 4 (the result) shows the first line is no longer present and there is still only 4 lines. The code you posted would display all 5 lines.
  11. well i can give u the data involved so you try it : div id is "chat" This is it simplified for the above information: document.getElementById(up.div).innerHTML += html.split("<br>").slice(0,3).join("<br/>"); 1) First split on <br/> 2) From the split get array of 0 to 3 3) Join 0 to 3 with <br/> which we initially split on 4) The result should now show:
  12. Hey Im using a js split to seperate lines then limit the amount to just 4 using slice but it won't do it. The data involved according to firebug is: {"lastupdate":1293751302,"html":"<a style=\"color:#AA0000;\" href=\"profile.php?view=1\">Username<\/a>: test <br\/>"} My split string is this: document.getElementById(up.div).innerHTML += data.html.split("<br/>").slice(0,3).join("<br/>"); But its not working - any one got a clue where its going wrong?
  13. Okay i added this : setInterval( "fillarea(area)", 2000); which now shows the image - thanks But it doesn't clear the image that it generated prior to generating it again in a new area. It just makes lots of the image at random places instead of clearing the one before generating the next. So that only one image will show at a time. Any ideas as to why this is ?
  14. Hey I have a function which i call every 2 seconds with a timeinterval but the issue i have is i can't seem to unset the image before re-assigning it the next time round, so currently it duplicates the image instead of only showing one of them. I tried the below function to clear it but it doesn't seem to like it. var area = 1; function fillarea(area){ var randomnumber=Math.floor(Math.random()*10); var randomnumber2=Math.floor(Math.random()*10); var newDiv = area+":"+randomnumber+":"+randomnumber2; if(last != undefined){ document.getElementById(last).innerHTML=""; } //its unset first time round so skip to avoid error document.getElementById(newDiv).innerHTML="<img src='tile.PNG'/>"; var last = newDiv; } setInterval( "fillarea()", 2000); Any idea where im going wrong ?
  15. Hey I have a JS value but it won't put the value in the brackets when i try to get the element by ID instead it is looking for the id named the same as the variable name. Here is what i got to explain better: function fill_div(v) { document.getElementById(v).innerHTML="<img src='untitle.png' style='width:100%;height:100%;'/>"; } var x = 0; var y = 3; var area = 1; window.onload = fill_div(area+':'+x+':'+y); It ends up looking for id v instead of id 1:0:3. Any idea how i correct this ?
  16. Hey Im trying to control movement of an image by using keypress. Yet the image remains static but i can't figure out where i've gone wrong. This is my script: <script> xpos2=400; ypos2=500; var timer; function moveleft(id){ xpos2=xpos2-15; obj=document.getElementById(id); obj.style.left=xpos2 +"px"; } function checkKey(){ var key; key = event.keyCode; if (key != 0){ switch(key){ case 97:moveleft("fone");break; } } } </script> </head> <body onkeypress="checkKey();"> <p><img id="fone" src="rocker.png" alt="rocket" > </p> </body> </html> Can some one explain how i do this which will also work on on all browsers? Thanks
  17. Hey, Does any one know of any example scripts on the net that use grid like panels where you use ajax to navigate a character walking around on the grid ? I'm trying to find a simple example so i can dissect it but having difficulty finding it on the net. I'm mainly just finding flash stuff but wish to do it in AJAX. Hope you can help. Thanks
  18. In order of bullet points: 1) Yes linux server 2) Yes same machine i only own one server 3) Umm currently i have "Post message" + "load chat logs" the latter only happens when ajax receives TRUE (aka a change has occured) 4)If i do not post message as it arrives it would not appear like a live chat (think like IRC)
  19. Well the way i get the info is select the rows and use a while loop - can't see any more efficient way but i still get about a 2 second ish lag after submitting a message to the time appears on screen =/
  20. Hey, Currently i have a chat room which calls my database quite frequently... so im thinking i can lower the heavy load from my server by taking it off MYSQL methods and using PHP read/write/delete methods of a text file. Now i have two issues with this... first one is: <?php $myFile = "chatlist.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = $Message.'<br/>'; fwrite($fh, $stringData); fclose($fh); ?> This writes the most recent line, how ever the next line that gets sent overwrites so theres only ever one line in the txt file =/ Secondly how on earth do you delete a specific line from the file efficiently ? Say for example a line which broke the rules some how like spam was posted etc. Hope you get shed light on this. Thanks
  21. Is there a way to use sort($Array) on numbers but reverse it. So instead of 1 to 3 itll do 3 to 1 ? AKA descending order? Can't find a function that would do unless theres a second sort function ? Thanks
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.