PHP обновляет страницу, когда происходит событие на стороне сервера

Я пытаюсь обновить страницу, когда другая страница получает и сохраняет некоторые данные без перезагрузки страницы. Я хочу использовать сохраненные данные, поэтому, если возможно, я бы хотел отправить их напрямую, а не делать вызов в базу данных. Фактические данные будут массивом с ключами.

В настоящее время я пытаюсь использовать Server Sent Events (SSE), чтобы отправить обновление веб-страницы, как только получающая страница сохранит данные. Я открыт для других решений, конечно.

Ниже приведен тестовый код PHP, написанный на платформе FuelPHP, над которой я работаю, чтобы попытаться выяснить проблему с упрощенными данными.

<?php
class Controller_Test extends Controller_Rest
{
public function get_receiverPage()
{
$data = input::get('test');
//do some stuff
if ($data['test'] == 'some circumstance')
{
/*
send $data to Server Side Event handler
or directly to display page if possible
*/
}
//continue with some other stuff
}

//this should send an update to /display
public function action_ServerSideEventHandler()
{
// this is some modified code from a sample.

//what headers should be used?
header("Content-Type: text/event-stream");

while (true)//should this be (isset($test))?
{
echo "data: ".$test."\n\n";
unset($test);

// no idea what to do with flush(), sleep, and related stuff
// the following is purely based off the sample

// flush the output buffer and send echoed messages to the browser
while (ob_get_level() > 0) {
ob_end_flush();
}
flush();
// break the loop if the client aborted the connection (closed the page)

if ( connection_aborted() ) break;
// sleep for 1 second before running the loop again

sleep(1);
}
}

}
?>

Вот основные функциональные возможности кода на стороне дисплея. В действительности я буду использовать данные для создания строки таблицы.

<script>


$(document).ready(function(){
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("/test/ServerSideEventHandler"); // /test/1 /receiverAPI/list
source.addEventListener('message', function(e) {
console.log(e.data);
}, false);
} else {
alert("Your browser does not support server-sent events. Please use any modern browser other than internet explorer/microsoft edge.");
}
});
</script>

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector