Pomoc - Szukaj - U¿ytkownicy - Kalendarz
Pe³na wersja: [AJAX] Shoutbox
Forum PHP.pl > Forum > XML, AJAX
Siela
Znalaz³em gdzie¶ na googlach tutorial jak w prosty i szybki sposób napisaæ shoutbox`a w ajax. Po wprowadzeniu kilku modyfikacji doda³em go do forum. Niestety okaza³ siê on pamiêcio¿erny. O ile forum zabiera³o ¶rednio 1,5% zu¿ycia serwera to przez tego shoutbox`a bra³ 5+%

Czy da siê go jako¶ zoptymalizowaæ?
A mo¿e znacie jaki¶ "l¿ejszy"?

  1. <?php
  2. var check=0;
  3.  function ajaxFunction(){
  4. var ajaxRequest;
  5.  
  6. try{
  7. // Opera 8.0+, Firefox, Safari
  8. ajaxRequest = new XMLHttpRequest();
  9. } catch (e){
  10. // Internet Explorer Browsers
  11. try{
  12. ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  13. } catch (e) {
  14. try{
  15. ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
  16. } catch (e){
  17. //browsers all not support, rare case
  18. alert("Your browser broke!");
  19. return false;
  20. }
  21. }
  22.  
  23. }
  24. return ajaxRequest;
  25. }
  26.  
  27.  
  28. function showData() {
  29. htmlRequest = ajaxFunction();
  30. if (htmlRequest==null){ // If it cannot create a new Xmlhttp object.
  31. alert ("Browser does not support HTTP Request");
  32. return;
  33. } 
  34.  
  35. htmlRequest.onreadystatechange = function(){
  36. if(htmlRequest.readyState == 4){
  37. document.getElementById("shoutarea").innerHTML = htmlRequest.responseText;
  38. if(!check)
  39. {
  40. check=1;
  41. showBottom();
  42. }
  43. }
  44. }
  45. htmlRequest.open("GET", "shout/outputinfo.php", true);
  46. htmlRequest.send(null);
  47.  
  48. }
  49.  
  50. showData();
  51. setInterval("showData()",10000);
  52.  
  53.  
  54. function saveData(){
  55. htmlRequest = ajaxFunction();
  56. if (htmlRequest==null){ // If it cannot create a new Xmlhttp object.
  57. alert ("Browser does not support HTTP Request");
  58. return;
  59. } 
  60.  
  61. htmlRequest.open('POST', 'shout/sendshout.php');
  62. htmlRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  63. htmlRequest.send('name='+document.shoutbox.shouter.value+'&message='+document.shoutbox.shouter_comment.value); 
  64.  
  65. document.shoutbox.shouter_comment.value = ''; // Updates the shout box’s text area to NULL.
  66. document.shoutbox.shouter_comment.focus(); // Focuses the text area.
  67. showData();
  68. showBottom();
  69.  
  70. } 
  71. function removeData(kokos){
  72. htmlRequest = ajaxFunction();
  73. if (htmlRequest==null){ // If it cannot create a new Xmlhttp object.
  74. alert ("Browser does not support HTTP Request");
  75. return;
  76. } 
  77. htmlRequest.open('POST', 'shout/removeshout.php');
  78. htmlRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  79. htmlRequest.send('koko='+kokos); 
  80. showData();
  81. showBottom();
  82. } 
  83.  
  84. function showBottom() {
  85. document.getElementById("shoutarea").scrollTop = document.getElementById("shoutarea").scrollHeight;
  86. }
  87. ?>
tommy4
Da siê.

Poczytaj o cache'owaniu.
vokiel
Pobieranie danych masz ustawione na stale co 10 sec (o ile dobrze policzylem winksmiley.jpg ).
Czyli za kazdym razem pobierany jest caly shoutbox, sprawdzaj co te 10 sec, czy cos sie zmienilo od czasu ostatniego sprawdzenia, jesli tak, pobieraj nowe wyniki, jesli nie, to nie rob nic.
Siela
Tyle, ¿e problemem nie jest transfer bo ten podskoczy³ o niewiele, lecz wykorzystanie procka serwera :/
To jest wersja lo-fi g³ównej zawarto¶ci. Aby zobaczyæ pe³n± wersjê z wiêksz± zawarto¶ci±, obrazkami i formatowaniem proszê kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.