服务器发送事件PHP不断发送过去的事件

我目前正在使用服务器发送的事件和PHP将信息从php服务器发送到javascript webclient。 它确实有效但是当我重新加载页面时,服务器发送所有最后的事件。 我想知道它是否将它们存储在任何地方以及如何阻止它这样做</ p>

下面是php代码。 我使用dbus来观看按键事件。</ p>

 &lt;?php 
header('Content-Type:text / event-stream');
header('Cache -Control:no-cache');

$ dbus = new Dbus(DBus :: BUS_SYSTEM);

$ dbus-&gt; addWatch(“nl.pcdata.display”,“displayKey”); \ n
do {
$ signal = $ dbus-&gt; waitLoop(100);
if($ signal instanceof DbusSignal){
if($ signal-&gt; matches)(“nl.pcdata.display”,“displayKey “)){
$ data = $ signal-&gt; getData() - &gt; getData();
echo”data:Address:{$ data [0]}; Button:{$ data [1]}。

retry:100

“;

flush();
break;
}
else {
echo”data:Geen data

“;

flush(); \ n}
}
} while(true);
?&gt;
</ code> </ pre>

javascript中的代码。</ p>

< pre> var source = new EventSource(“textinput.php”);
source.onmessage = function(event){
console.log(typeof event.data);
var result = event.data。 split(“;”);
console.log(result);
docum ent.getElementById(“items”)。innerHTML + =“&lt; li&gt;” + event.data +“&lt; / li&gt;”;
};
</ code> </ pre>

所以代码执行它应该做的事情。 例如,当我按下okay按钮时,服务器会将其发送到web客户端。</ p>

它看起来像这样</ p>

控制台输出:</ p>

</ p>

当我重新加载页面时,它再次输出。 我需要的是当我重新加载页面时,它只显示从该点开始按下的键。</ p>
</ div>

展开原文

原文

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:

enter image description here

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐