Answer the question
In order to leave comments, you need to log in
SSE php and javascript
I am working with the EventSource library.
It is necessary to update the data on the page in realtime.
Customer:
<script type="text/javascript">
var es = new EventSource("/games/game/updateProposal/?id=<?=$proposal->proposal_id?>");
var open = function (event)
{
console.log('Открытие');
};
var listener = function (event)
{
console.log('OK');
//event.data = JSON.parse(event.data);
console.log(event.data);
};
var error = function (event)
{
console.log('Ошибка');
};
es.addEventListener("open", open);
es.addEventListener("message", listener);
es.addEventListener("error", error);
</script>
{
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache'); // recommended to prevent caching of event data.
$lastEventId = floatval(isset($_SERVER["HTTP_LAST_EVENT_ID"]) ? $_SERVER["HTTP_LAST_EVENT_ID"] : 0);
if ($lastEventId == 0)
{
$lastEventId = (isset($_GET["key"])) ? (string)$_GET["key"] : 0;
}
$w = array(
array('test1'=>1),
array('test2'=>2),
array('test3'=>3),
);
$data = ProcessingData::load()->getJson($w);
echo "id: {$lastEventId}" . PHP_EOL;
echo "data: $data" . PHP_EOL;
echo PHP_EOL;
sleep(5);
ob_flush();
flush();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question