<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          PHP SSEPHP 實現(xiàn) HTML5 的服務(wù)器發(fā)送事件

          聯(lián)合創(chuàng)作 · 2023-09-29 07:33

          PHP SSE: Server-sent Events,一個簡單有效的庫,通過 PHP 實現(xiàn)了 HTML5 的服務(wù)器發(fā)送事件,用于實時從服務(wù)器推送事件到客戶端,比 Websocket 更容易。

          要求:PHP 5.4 or later

          示例代碼

          Javascript demo

          Client: receiving events from the server

          //withCredentials=true: pass the cross-domain cookies to server-side
          var source = new EventSource("http://127.0.0.1:9001/push.php", {withCredentials:true});
          source.addEventListener("new-msgs", function(event){
              console.log(event.data);//get data
          }, false);

          PHP demo

          Server: sending events from the server by pure php

          include './vendor/autoload.php';
          
          use Hhxsv5\SSE\SSE;
          use Hhxsv5\SSE\Update;
          
          header('Content-Type: text/event-stream');
          header('Cache-Control: no-cache');
          header('Connection: keep-alive');
          header('X-Accel-Buffering: no');//Nginx: unbuffered responses suitable for Comet and HTTP streaming applications
          
          (new SSE())->start(new Update(function () {
              $id = mt_rand(1, 1000);
              $newMsgs = [
                  [
                      'id'      => $id,
                      'title'   => 'title' . $id,
                      'content' => 'content' . $id,
                  ],
              ];//get data from database or servcie.
              if (!empty($newMsgs)) {
                  return json_encode(['newMsgs' => $newMsgs]);
              }
              return false;//return false if no new messages
          }), 'new-msgs');
          瀏覽 21
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  欧美精品在线自偷自拍 | 福利在线色 | 亚欧精品久久久久久久久久久 | 国产成人免费高清视频 | 97丁香五月天 |