PHP로 JS Server-Sent_Events를 사용하는 시스템을 설정 중입니다. 그리고 잘 작동합니다. 그러나 모든 일에 오류 이벤트가 발생하면 한 번 발생하고 이유를 이해할 수 없습니다.js server를 사용하여 PHP로 이벤트를 보낸 지 정하지 않은 sse 오류가 나타나는 이유는 무엇입니까?
내 JS는 :
는var source = new EventSource("serverSideEvents.php");
source.onmessage = function(event) {
var data = $.parseJSON(event.data);
$(data).each(function(){
doSomethingWith(this); //works as expected
});
};
source.onerror = function(event) {
console.log(event); //fires every few seconds don't know why
};
내 PHP는 :
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
sendUpdates();
function sendUpdates() {
$controller = new someController();
$out = $controller->Run();
foreach($out as $msg)
sendSSEMessage($msg['id'], 'data:'.json_encode($msg));
}
function sendSSEMessage($id, $data){
echo "id: $id \n$data \n\n";
ob_flush();
flush();
}
이는 CLI 또는 PHP는 오류가 발생하지 않습니다 브라우저, 아직 JS SSE의 오류 이벤트에, 확인 작업은 모든 시간을 발사 PHP가 실행됩니다. 콘솔에서 오류가 발생했습니다.
Event {isTrusted: true, type: "error", target: EventSource, currentTarget: EventSource, eventPhase: 2, …}
bubbles:false
cancelBubble:false
cancelable:false
composed:false
currentTarget:
EventSource {url: "http://localhost:7790/serverSideEvents.php", withCredentials: false, readyState: 0, onopen: null, onmessage: ƒ, …}
defaultPrevented:false
eventPhase:0
isTrusted:true
path:[]
returnValue:true
srcElement:EventSource {url: "http://localhost:7790/serverSideEvents.php", withCredentials: false, readyState: 0, onopen: null, onmessage: ƒ, …}
target:EventSource {url: "http://localhost:7790/serverSideEvents.php", withCredentials: false, readyState: 0, onopen: null, onmessage: ƒ, …}
timeStamp:1129162.5250000001
type:"error"
__proto__:Event
명확한 내용 : 예상대로 작동합니다. 내가 필요한 서버 메시지를받습니다. 또한 오류 이벤트가 트리거됩니다.
아마도 CORS 문제일까요? –