@denism300

Почему возникает ошибка при SSE?

Знакомлюсь с технологией SSE, на локалхосте стоит OSPanel, набросал следующий код по примерам из инета:
index.html
<html lang="ru">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <header>
            <h1>This is SSE Test</h1>
        </header>
        <main>
            <div id="content">
                <p>SSE here: <span></span></p>
            </div>
        </main>
    </body>
    <script src="index.js" type="text/javascript"></script>
</html>

index.js
const evtSource = new EventSource("sse.php");

evtSource.onmessage = function (event) {
    const container = document.getElementById('content').querySelector('span');

    container.innerHTML = "message: " + event.data;
};

evtSource.onerror = function (error) {
    console.error("EventSource failed: ", error);
};

sse.php
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

while (true) {
    $time = date('r');
    echo "data:Time: $time\n\n";
    ob_flush();
    flush();
    sleep(1);

    if (connection_aborted()) {
        break;
    }
}
ob_end_flush();

в итоге, в консоли браузера получаю ошибку
EventSource's response has a MIME type ("application/x-httpd-php") that is not "text/event-stream". Aborting the connection.

если скрипт запустить в curl, то срабатывает корректно - выводит дату и время
  • Вопрос задан
  • 106 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы