Long Polling Doesn’t Do Timestamp GET

Asked

Viewed 88 times

3

I picked up this example on the blog http://rberaldo.com.br/server-push-long-polling-php-ios/

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();
});

HE DOESN’T DO THE TIMESTAMP ISSET, WHERE THE PROBLEM IS?

1 answer

0

If you’re trying to catch the timestamp by the date reply to $.getJSON, then why didn’t you answer with the timestamp?

The correct part of your code response should be:

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

I don’t know where the content in your code (apparently you do not return any data with that name) so I defined it as the $publication_id.

  • Here is the problem,if ($stmt->rowCount() > 0), if the query result is not found, then it will continue the loop without the timestamp

  • Then you have to change the logic of the front-end if you don’t get an answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.