<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                訂閱發布主要用于廣播系統,例如聊天系統中的群聊,消息推送,同步操作等等;這里以同步操作為例子作為講解 ![訂閱發布](https://box.kancloud.cn/c223c3affae5211b70410259cf1a1572_814x471.png) 圖片來源于https://www.cnblogs.com/JasonLeemz/p/5116814.html ##### 大概流程如下: - pc端游覽器與websocket建立長連接 - websocket訂閱redis (subscribe channel),一旦channel有數據,websocket主動推送數據給pc端游覽器 - 手機設備修改數據,并執行發布 (publish channel message) - websocket獲取到channel的message,主動推送給pc游覽器 ##### 注意點: - redis的訂閱發布使用php內置的socket,默認超時時間為60秒,需要設置ini_set('default_socket_timeout', -1); - php的redis擴展為阻塞IO,當websocket訂閱redis,整個進程會阻塞,導致服務端的websocket不能處理其他事件,從而無法記錄長連接fd,解決方法用異步redis ##### 下面是一個簡單的基于swoole異步redis實現的websocket服務端 ```php class WebsocketServer { public $fdMaps = []; /** @var \Swoole\WebSocket\Server */ public $ws; public function __construct() { $this->ws = new Swoole\WebSocket\Server('0.0.0.0', 9503); $this->ws->on('open', [$this, 'open']); $this->ws->on('close', [$this, 'close']); $this->ws->on('workerStart', [$this, 'workerStart']); $this->ws->on('message', [$this, 'message']); $this->ws->set([ 'worker_num' => 1, 'daemonize' => 0, ]); $this->ws->start(); } public function message($server, $frame) { $server->push($frame->fd, "hello"); } public function workerStart(\Swoole\WebSocket\Server $server, $workerID) { $client = new \Swoole\Redis(); $client->on('message', function ($client, $result) use ($server) { if ($result[0] == 'message') { foreach ($server->connections as $fd) { $server->push($fd, $result[2]); } } }); $client->connect('127.0.0.1', 6379, function ($client, $result) { $client->subscribe('channel'); }); } public function open($server, $request) { echo "fd $request->fd connect \n"; $this->fdMaps[$request->fd] = $request->fd; } public function close($server, $fd) { if (isset($this->fdMaps[$fd])) { unset($this->fdMaps[$fd]); echo "fd $fd close \n"; } else { echo "error\n"; } } } new WebsocketServer(); ``` html代碼: ```html <html> <body> <h1>Redis publish/subscribe</h1> <div id="show-list"></div> </body> </html> <script type="text/javascript" src="/chat/js/jquery.min.js"></script> <script> $(function () { var ws = new WebSocket('ws://127.0.0.1:9503'); ws.onopen = function () { console.log('connect success'); }; // 接受消息 ws.onmessage = function (evt) { console.log(evt.data); var html = '<h3>' + evt.data + '</h3>'; $('#show-list').append(html); }; ws.onclose = function () { console.log('connect close'); }; }); </script> ``` 調試: ```bash 127.0.0.1:6379> publish channel hello (integer) 1 # 響應的pc游覽器會顯示hello ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看