longpolling.js
function getContent( timestamp )
{
var queryString = {"timestamp" : timestamp};
$.ajax({
type: "GET",
url: '/php-long-polling-master/server/server.php',
data: queryString,
sucess: function ( data ) {
var obj = jQuery.parseJSON( data )[0];
$( '#response' ).html( obj.content );
// reconnect with other timestamp from **server.php**
getContent( obj.timestamp );
}
});
}
$( document ).ready ( function ()
{
getContent();
});
server.php
<?php
require 'pdo.php';
set_time_limit(0);
while ( true )
{
$requestedTimestamp = isset ( $_GET [ 'timestamp' ] ) ? (int)$_GET [ 'timestamp' ] : time();
clearstatcache();
$stmt = $pdo->prepare( "SELECT * FROM publication WHERE publication_time >= :requestedTimestamp" );
$stmt->bindParam( ':requestedTimestamp', $requestedTimestamp );
$stmt->execute();
$rows = $stmt->fetchAll( PDO::FETCH_ASSOC );
if ( count( $rows ) > 0 )
{
$json = json_encode( $rows );
var_dump($rows);
echo $json;
break;
} else {
sleep( 2 );
continue;
}
}
?>
The problems:
-
longpolling.js don't have a timestamp, the log shows: get /directory/directory/server.php instead server.php?timestamp=TimeInMilisseconds
-
If don't have a timestamp, the server.php never will show results :|
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire