Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [AJAX] Shoutbox
Siela
post 28.05.2008, 08:08:38
Post #1





Grupa: Zarejestrowani
Postów: 27
Pomógł: 1
Dołączył: 28.02.2005
Skąd: Gdańsk

Ostrzeżenie: (0%)
-----


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. ?>


Ten post edytował Siela 28.05.2008, 08:11:50
Go to the top of the page
+Quote Post
tommy4
post 28.05.2008, 08:26:10
Post #2





Grupa: Zarejestrowani
Postów: 288
Pomógł: 12
Dołączył: 2.12.2005

Ostrzeżenie: (0%)
-----


Da się.

Poczytaj o cache'owaniu.
Go to the top of the page
+Quote Post
vokiel
post 28.05.2008, 09:49:11
Post #3





Grupa: Zarejestrowani
Postów: 2 592
Pomógł: 445
Dołączył: 12.03.2007

Ostrzeżenie: (0%)
-----


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.


--------------------
Go to the top of the page
+Quote Post
Siela
post 28.05.2008, 11:15:37
Post #4





Grupa: Zarejestrowani
Postów: 27
Pomógł: 1
Dołączył: 28.02.2005
Skąd: Gdańsk

Ostrzeżenie: (0%)
-----


Tyle, że problemem nie jest transfer bo ten podskoczył o niewiele, lecz wykorzystanie procka serwera :/
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 29.06.2025 - 21:33