samedi 28 février 2015

Longpolling (I think) is killing my local server


I am running a longpolling script to grab data from the database. It was working fine until moving my script to an MVC.


I have viewed the chrome developer tool and it's showing nothing in there, but the page just carries on loading, and when I go to refresh it won't load, I have to shit down my xampp server or close my browser... Here's my script:



<?php

class SystemController extends Controller
{

public function lastbid()
{

set_time_limit(0);


// main loop
while (true) {


//get the product info
$getbidresult = ProductModel::bidprice(Request::get('item'));

// if ajax request has send a timestamp, then $last_ajax_call = timestamp, else $last_ajax_call = null
$last_ajax_call = Request::get('timestamp');


// get timestamp of when file has been changed the last time
$lastbid = isset($getbidresult->timestamp) ? $getbidresult->timestamp : 0;

// if no timestamp delivered via ajax or data.txt has been changed SINCE last ajax timestamp
if ($last_ajax_call == null || $lastbid > $last_ajax_call) {

// put last bid info into an array
$result = array(
'bidamount' => isset($getbidresult->amount) ? System::escape($getbidresult->amount): 0,
'timestamp' => System::escape($lastbid)
);

// encode to JSON, render the result (for AJAX)
$json = json_encode($result);
echo $json;

// leave this loop step
break;

} else {
// wait for 1 sec (not very sexy as this blocks the PHP/Apache process, but that's how it goes)
sleep(10);
continue;
}
}

}
}


This is how I am grabbing the data with JS.



<script type="text/javascript">
function getContent(timestamp)
{
var queryString = {
'timestamp': timestamp
};
$.ajax(
{
type: 'GET',
url: '<?php echo Config::get('URL'); ?>system/lastbid?item=<?php echo System::escape($recentitem->id); ?>',
data: queryString,
success: function(data)
{
var obj = jQuery.parseJSON(data);
$('#bidprice-<?php echo System::escape($recentitem->id); ?>').html(obj.bidamount);
getContent(obj.timestamp);
}
});
}
$(function()
{
getContent();
});
$(document).ready(function() {
});
</script>


I've looked in apache logs with no avail unless I am looking in the wrong place. Does anything in the code look out of place, It doesn't to my knowledge but I may be overlooking something.


I have the script in a foreach, so I can initiate the div, for each product.





Aucun commentaire:

Enregistrer un commentaire