Long Polling does not timestamp GET

3

I took this example on the blog link

Server.php

<?php
header('Content-type: application/json');
require 'pdo.php';
set_time_limit(0);
while ( true )
{
    $requestedTimestamp = isset( $_GET['timestamp'] ) ? (int)$_GET['timestamp'] : 0;

    $stmt = $pdo->prepare( "SELECT * FROM publication WHERE publication_time = :requestedTimestamp" );

    $stmt->bindParam( ':requestedTimestamp', $requestedTimestamp );
    $stmt->execute();



    if ($stmt->rowCount() > 0)
    {
    while ($rowpublication = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $publication_id  = $rowpublication['publication_id'];
    }
        $json = json_encode( $publication_id );
        echo $json;
        break;
    }
    else
    {
        sleep( 2 );
        continue;
    }
}
?>

Client.js

function getContent( timestamp )
{
    var queryString = { 'timestamp' : timestamp };

    $.get ( '/php-long-polling-master/server/server.php' , queryString , function ( data )
    {
        var obj = jQuery.parseJSON( data );
        $( '#response' ).html( obj.content );

        // reconecta ao receber uma resposta do servidor
        getContent( obj.timestamp );
    });
}

$( document ).ready ( function ()
{
    getContent();
});
  

IT DOES NOT DO THE TIMESTAMP ISSET, WHERE IS THE PROBLEM?

    
asked by anonymous 11.08.2015 / 21:18

1 answer

0

If you're trying to get timestamp through date response to $.getJSON , then why did not you respond with timestamp ?

The correct snippet of your code response should be:

 $json = json_encode(array(
     'content' => $publication_id, 
     'timestamp' => $requestedTimestamp
));

I do not know where content comes from in your code (apparently you do not return any data with that name) so I set it to $publication_id .

    
17.08.2015 / 16:14