I am currently working with Server-sent Events and PHP to send information from the php server to the javascript webclient. It does work however when I reload the page the server sends all the last events. I wonder if it stores them anywhere and how I could stop it from doing that
Below is the php code. I use dbus to watch for an key press event.
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$dbus = new Dbus(DBus::BUS_SYSTEM);
$dbus->addWatch("nl.pcdata.display", "displayKey");
do {
$signal = $dbus->waitLoop(100);
if($signal instanceof DbusSignal){
if($signal->matches("nl.pcdata.display","displayKey")){
$data = $signal->getData()->getData();
echo "data:Address:{$data[0]};Button:{$data[1]}.
retry: 100
";
flush();
break;
}
else{
echo "data:Geen data
";
flush();
}
}
}while(true);
?>
And the code in javascript.
var source = new EventSource("textinput.php");
source.onmessage = function(event){
console.log(typeof event.data);
var result = event.data.split(";");
console.log(result);
document.getElementById("items").innerHTML += "<li>" + event.data + "</li>";
};
So the code does what its supposed to do. for example when I press the okay button the server sends that to the webclient.
It looks like this then
Console output:
When I reload the page it outputs that again. What I need is when i reload the page that it just show the keys pressed from that point on.