dqsp60748 2017-09-05 13:49
浏览 68

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

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料