var evtSource = new EventSource('sse.php');
evtSource.onopen = function(evt) {
}
evtSource.onerror = function(evt) {
}
evtSource.onmessage = function(evt) {
... evt.data ...
}
<?php
header("Content-Type: text/event-stream");
$count = 10;
while ($count--) {
echo $count,"\n";
while (ob_get_level() > 0) {
ob_end_flush();
}
flush();
if ( connection_aborted() ) break;
sleep(1);
}
event: message // 이벤트 명. message 는 evtSource.onmessage 처럼 사용 가능
data: START // 데이터 문자열 이 다음엔 줄바꿈 2번(\n\n)으로 구분한다.
id: countdown_10 // 옵션값. lastEventId
event: countdown // 이벤트 명. evtSource.addEventListener('countdown',function(evt){}); 처럼 사용
data: 10