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条回答

    报告相同问题?

    悬赏问题

    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 oracle集群安装出bug
    • ¥15 关于#python#的问题:自动化测试