nepster-web
@nepster-web

SSE, постоянно попадает в метод ошибки

Использую вот такую библиотеку для SSE запросов: https://github.com/Yaffle/EventSource/

Сервер:
header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache'); // recommended to prevent caching of event data.
    header("Access-Control-Allow-Origin: *");

    ...

    //echo ":" . str_repeat(" ", 2048) . "\n"; // 2 kB padding for IE
    echo "retry: 2000\n";
    echo "id: {$lastEventId}" . PHP_EOL;
    echo "data: " . ProcessingData::load()->getJson($updateProposals) . PHP_EOL;
    echo PHP_EOL;

    sleep(2);

    ob_flush();
    flush();


Клиент
var listener = function (event) {
                var type = event.type;

                console.log(type + ": " + (type === "message" ? event.data : es.url));

              };

              es.addEventListener("open", listener);
              es.addEventListener("message", listener);
              es.addEventListener("error", listener);


Проблема в том, что всегда попадаем в es.addEventListener("error", listener);

Пример: Посылаю запрос на верный урл, данные с сервера приходят, попадаем в message, все хорошо, но и непонятно почему в error тоже попадаем Посылаю запрос на не верный url, приходит 404 ошибка, в message не попадаем, в error попадаем.

Подскажите пожалуйста почему я постоянно попадаю в error?
  • Вопрос задан
  • 2824 просмотра
Пригласить эксперта
Ответы на вопрос 1
avalak
@avalak
Неужели так сложно прочитать спеки? Там же всё подробно расписанно.

When a user agent is to reestablish the connection, the user agent must queue a task to set the readyState attribute to CONNECTING and fire a simple event named error at the EventSource object, and then, after a delay equal to the reconnection time of the event source, if the readyState attribute is still set to CONNECTING, once again do a potentially CORS-enabled fetch of the absolute URL of the event source resource, with the mode being Use Credentials, and the origin being the same as the origin used in the original request triggered by the EventSource() constructor, and process the resource obtained in this fashion, if any, as described in this section.


1. Соединение устанавливается. => open
2. Данные принимаются. => message
3. php дохнет => соединение закрывается. EventSource бросает error и пытается восстановить соединение.
Ответ написан
Комментировать
Ваш ответ на вопрос

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

Войти через центр авторизации
Похожие вопросы
summer Ярославль
от 100 000 до 140 000 ₽
КРАФТТЕК Санкт-Петербург
от 60 000 до 80 000 ₽
19 апр. 2024, в 23:00
5000 руб./за проект
19 апр. 2024, в 20:43
20000 руб./за проект