简体   繁体   English

服务器发送的事件停止(以前正在工作)到达浏览器

[英]Server sent events stopped (was previously working) reaching the browser

I have a bit of a mystery on my hands and would appreciate some assistance debugging this.我手头上有一些谜团,希望得到一些帮助来调试它。

I have this script:我有这个脚本:

//------------ Server sent event stream for sensor events---------------- 
var evtSource = new EventSource("event_manager.php");
evtSource.onmessage = function(e) 
{
    var newElement = document.createElement("li");

    console.log(e.data);
    newElement.innerHTML = e.data;
    eventList.appendChild(newElement);

    //------------ Splits received event data for further use ----------------
    var split_data = e.data.split(" ");
    if (split_data[3] == "Potentiometer")
    {
        document.getElementById("livingRoomTemp").innerHTML = split_data[4] + "°";
    }

    //------------ Keeps event list scrolled to the latest event ----------------
    var objDiv = document.getElementById("eventBar");
    objDiv.scrollTo(0, objDiv.scrollHeight);
}

That opens up an event stream with this PHP file:用这个 PHP 文件打开一个事件流:

<?php

header("Content-Type: text/event-stream\n\n");
require("../includes/config.php"); 

while (1) {

    $rows = query("SELECT * FROM `event_log` WHERE `displayed` = 0");

    foreach ($rows as $event)
    {
        echo "time:"  . $event["time"]  . "controller:" . $event["controller"]  . "sensor:" .$event["sensor"]  . "data:" .$event["data"]  . " " ."\n\n";

        query("UPDATE `event_log` SET `displayed` = 1 WHERE `id` = ?", $event["id"]);
    }

    ob_end_flush();
    flush();
    sleep(1);
}

?>

All of this was working perfectly before and, to my knowledge, I haven't changed a thing with either portion but for some reason my .onmessage function doesn't execute anymore.所有这些之前都运行良好,据我所知,我没有对任何部分进行任何更改,但由于某种原因,我的.onmessage函数不再执行。

I can curl into the event stream PHP file and watch it working as intended, sending the echo to my terminal but for whatever reason the browser just does nothing.我可以卷入事件流 PHP 文件并观察它按预期工作,将echo发送到我的终端,但无论出于何种原因,浏览器什么都不做。 The query function you see there simply sends a SQL query to MySQL.您在那里看到的query函数只是向 MySQL 发送一个 SQL 查询。 The SSE is clearly running since the query functions are having the intended effect on the MySQL table. SSE 显然正在运行,因为查询功能对 MySQL 表产生了预期的影响。 I see no errors in the network log of the browser (Iceweasel)and no logs to the console as I would expect.我在浏览器 (Iceweasel) 的网络日志中没有看到任何错误,也没有像我期望的那样在控制台中看到任何日志。 I can even see the network log updating the SSE at the interval that I specified.我什至可以看到网络日志以我指定的时间间隔更新 SSE。 It's driving me insane.它让我发疯。

I have no idea to fix (unbreak) this and would really appreciate your help.我不知道解决(破解)这个问题,非常感谢您的帮助。

Extra info:额外信息:

LAMP server on a Raspberry Pi 2 Raspberry Pi 2 上的 LAMP 服务器

Raspbian (Jessie, I think. The latest regardless) Raspbian(Jessie,我想。不管是最新的)

Browser: Iceweasel (firefox)浏览器:Iceweasel (firefox)

The Pi functions as both UI and server for a DIY home automation system that communicates with Arduino microcontrollers (only one at the moment) that are also set up as servers controlling lights, reading from sensors etc. The data from the arduino is sent to a MySQL database and the SSE above polls that database to see if the latest reading has been displayed in the UI and if not sends it to the browser and updates it as sent. Pi 用作 DIY 家庭自动化系统的 UI 和服务器,该系统与 Arduino 微控制器(目前只有一个)进行通信,这些微控制器也被设置为控制灯光、从传感器读取等的服务器。来自 arduino 的数据被发送到MySQL 数据库和上面的 SSE 轮询该数据库以查看最新读数是否已显示在 UI 中,如果未显示,则将其发送到浏览器并在发送时更新它。

This was one of the first things I set up in this project and was working as intended until earlier today.这是我在这个项目中设置的第一件事,直到今天早些时候才按预期工作。 Please let me know if I left out any important data and I'll rectify that immediately.如果我遗漏了任何重要数据,请告诉我,我会立即纠正。

Solved it.解决了。

Tore the site down to nothing but a div, the script and PHP file and the problem persisted.将站点撕成一个 div、脚本和 PHP 文件,问题仍然存在。

Turns out that this was the culprit:事实证明,这是罪魁祸首:

echo "time:" . $event["time"]...

The first part of the response needed to be prefaced with:响应的第一部分需要以以下开头:

echo "data:" . $event["time"]...

Why?为什么? I'm not sure.我不知道。 I need to educate myself more on SSEs.我需要更多地了解 SSE。

Regardless, the root of the problem here was careless coding.无论如何,这里问题的根源在于粗心的编码。 I made a change in anticipation of another feature and didn't check that it still worked afterwards.我对另一个功能的预期进行了更改,但之后没有检查它是否仍然有效。 I then got sidetracked with something else and forgot about the change I made.然后我被其他事情分散了注意力,忘记了我所做的改变。 This was compounded by the fact that I didn't keep a whole project backup.我没有保留整个项目备份的事实使情况更加复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM