Bu script sohbet verimli?

0 Cevap php

i php ve ajax kullanarak bir sohbet yaptı, ve ben yeni mesajlar için veritabanını kontrol etmek için bir süre döngü kullanıyorum.

Bu mesajı alır kodu:

//retrive message

function update(){

$(document).ready(function(){

$.ajax({

async: true,

type: "POST",

url: "listen.php",

success: function(data){

$("#myp").before(data);

},

complete: function(){

window.setTimeout("update();",100);
}

});

});

};


//wating for new message

<?php
include_once("connect.php");

$type="";

while($type!=='n'){


usleep(1000);

$search=mysql_query("SELECT * from chat ORDER BY id DESC LIMIT 1");

$row=mysql_fetch_assoc($search);

$type=$row['type'];

$id=$row['id'];

}

echo $row['message'] . "<br/>";


mysql_query("UPDATE chat SET type='o' WHERE id=$id");


?>

now this works fine,the php file constantly checks to see if there are any new messages, the update function starts when the page is loaded and waits for the response. but is it efficient? if i would use this on a website, im afraid it would stress the server too much, because of the while loop. does anyone know of a way, to make that while loop more server friendly?

0 Cevap