SSE:意外的MIME类型错误

问题描述 投票:1回答:1

我正在使用PHP文件作为SSE连接的工作程序。这是我的PHP文件:

<?php

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

$mysqli = new mysqli(* data *);
$listings = $mysqli->query("SELECT listing_id, price, buyer_id FROM listings WHERE schedule_requested = '1' AND finished = '0'");

$to_check = array();
while ($li = $listings->fetch_array()) {
    $to_check[] = array($li["listing_id"], $li["price"], $li["buyer_id"]);
}

while (true) {
    $change = 0;
    $listings_2 = $mysqli->query("SELECT listing_id, price, buyer_id FROM listings WHERE schedule_requested = '1' AND finished = '0'");
    $different = array();
    while ($li_2 = $listings_2->fetch_array()) {
        foreach ($to_check as &$li_check) {
            if ($li_check[0] === $li_2["listing_id"]) {
                if (intval($li_2["price"]) !== intval($li_check[1])) {
                    $li_check = array($li_2["listing_id"], $li_2["price"], $li_2["buyer_id"]);
                    $different[] = $li_check;
                    $change = 1;
                }
                break;
            }
        }
    }
    if ($change === 1) {
        echo "data: " . json_encode($different) . PHP_EOL;
        echo PHP_EOL;
        flush();
    }
    sleep(1);
}

?>

即使有指定内容类型的标头,当我尝试使用该文件时,也会收到错误消息:EventSource的响应的MIME类型(“ text / html”)不是“ text / event-stream”。中止连接。无论如何,当我对同一文件使用以下代码时(注释无限while循环),我没有收到该错误。

<?php

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

$mysqli = new mysqli(* data *);
$listings = $mysqli->query("SELECT listing_id, price, buyer_id FROM listings WHERE schedule_requested = '1' AND finished = '0'");

$to_check = array();
while ($li = $listings->fetch_array()) {
    $to_check[] = array($li["listing_id"], $li["price"], $li["buyer_id"]);
}

/*while (true) {
    $change = 0;
    $listings_2 = $mysqli->query("SELECT listing_id, price, buyer_id FROM listings WHERE schedule_requested = '1' AND finished = '0'");
    $different = array();
    while ($li_2 = $listings_2->fetch_array()) {
        foreach ($to_check as &$li_check) {
            if ($li_check[0] === $li_2["listing_id"]) {
                if (intval($li_2["price"]) !== intval($li_check[1])) {
                    $li_check = array($li_2["listing_id"], $li_2["price"], $li_2["buyer_id"]);
                    $different[] = $li_check;
                    $change = 1;
                }
                break;
            }
        }
    }
    if ($change === 1) {
        echo "data: " . json_encode($different) . PHP_EOL;
        echo PHP_EOL;
        flush();
    }
    sleep(1);
}*/

echo "data: " . json_encode($different) . PHP_EOL;
echo PHP_EOL;
flush();

?>

我不明白为什么会这样。

php server-sent-events
1个回答
0
投票

SSE标准明确指出,如果响应中的[[not包含文本/事件流,则MUST会使连接出错。

具有文本/事件流(或其他受支持的类型)以外的内容类型的HTTP 200 OK响应必须导致用户代理连接失败。

SSE protocol

© www.soinside.com 2019 - 2024. All rights reserved.