SSE(Server-Seend Event)サーバがデータを転送する新しい方式

771 ワード

Web APIインターフェース:EventSource
EventSourceインターフェースは、サーバから送信されたイベントを受信するために使用されます。それはHTTPを通してサーバーに接続して、text/event-stream形式でイベントを受信して、接続を閉じません。
サービスコード
\r
"; ??>
フロントエンドコード
let source;

function init(arg) {
  source = new EventSource('http://127.0.0.1/sse/data.php');
  source.onopen = () => {
    console.log("     ", this.readyState);
  }
  source.onmessage = (event) => {
    console.log("         :", event.data);
  }

  source.onerror = () => {
    console.log('');
  }
}
init();