我需要你的帮助。
我正在建立一个社交网络,我在大多数应用程序中使用Long Polling with PHP,但我意识到当你在Chrome中打开多个标签时,代码很慢而且无法运行。使用打开的翻盖,代码可以正常工作。
有人有灯吗?
PHP`
$timestart = time();
$pdo = new PDO("mysql:host=localhost;dbname=social", "social", "senha");
$pdo->exec("SET CHARACTER SET utf8");
if(isset($_POST['timestamp'])){
$timestamp = $_POST['timestamp'];
}else{
$pega_time = $pdo->prepare("SELECT NOW() as now");
$pega_time->execute();
$row = $pega_time->fetchObject();
$timestamp = $row->now;
}
$sql = $pdo->prepare("SELECT postagens.id as postagem, n postagens.likestimestamp FROM postagens WHERE postagens.likestimestamp > '$timestamp'");
$newData = false;
$postagens = array();
while(!$newData && (time()-$timestart)<15){
$sql->execute();
while($row = $sql->fetchAll(PDO::FETCH_ASSOC)){
$postagens = $row;
$newData = true;
break;
}
usleep(6000);
}
$pega_time = $pdo->prepare("SELECT NOW() as now");
$pega_time->execute();
$row = $pega_time->fetchObject();
$timestamp = $row->now;
$data = array('postagens' => $postagens, 'timestamp' => $timestamp);
echo json_encode($data);
exit;
JQUERY
pegaLikes();
function pegaLikes(timestamp){
var data = {};
var texto = {};
if(typeof timestamp != 'undefined') {
data.timestamp = timestamp;
}
$.post('assets/serverlikes.php', data, function(res){
for(i in res.postagens){
$.get("tete.php?likes=1&id="+res.postagens[i].postagem, function(result) { $('#likesnumeros'+res.postagens[i].postagem).html(result);
});
$.get("like.php?id="+res.postagens[i].postagem, function(result) { $('#likar'+res.postagens[i].postagem).html(result);
});
}
pegaLikes(res.timestamp);
}, 'json'); }