<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                PHP 在之前的某一段時間里是被認為無法處理好協議層面的,但是隨著Workerman,Swoole 以及目前風頭正上的 MEEPOPS 的出現,徹底打破了這一局面,讓很多人發現原來 PHP 能做的事越來越多。 接下來本章將詳細介紹如何用 swoole_websocket_server 和 ThinkPHP5 的命令行來建立 websocket 服務器。 本章沒有使用think-swoole Composer 包。 創建 ThinkPHP5 自定義命令行 在 ThinkPHP5 中推薦使用命令行工具來管理運行不需要外部訪問的程序,如 http 服務器啟動程序,websocket 服務器啟動程序,crontab(計劃任務/定時器)等 1.創建命令行類 - - 創建application/console/WebSocket.php文件 ```php <?php namespace app\Console; use think\console\Command; use think\console\Input; use think\console\Output; class WebSocket extends Command { // 命令行配置函數 protected function configure() { // setName 設置命令行名稱 // setDescription 設置命令行描述 $this->setName('websocket:start')->setDescription('Start Web Socket Server!'); } // 設置命令返回信息 protected function execute(Input $input, Output $output) { $output->writeln("WebSocket: Start.\n"); } } ``` - 2.修改配置文件 文件所在 application/command.php ~~~ <?php return [ 'app\console\WebSocket', ]; ~~~ 這時在項目根目錄輸入命令行就可以看到已經配置好的命令行類及描述 編寫 Web Socket 邏輯代碼 接下來我們就可以正式編寫 web socket 服務器的代碼了,Swoole 幫我們內置了 web socket 服務器支持,通過幾行 PHP 代碼就可以寫出一個異步非阻塞多進程的 web scoket 服務器。 在application/console/WebSocket.php文件中繼續編寫代碼 ```php <?php namespace app\Console; use think\console\Command; use think\console\Input; use think\console\Output; class WebSocket extends Command { // Server 實例 protected $server; protected function configure() { $this->setName('websocket:start')->setDescription('Start Web Socket Server!'); } protected function execute(Input $input, Output $output) { // 監聽所有地址,監聽 10000 端口 $this->server = new \swoole_websocket_server('0.0.0.0', 10000); // 設置 server 運行前各項參數 // 調試的時候把守護進程關閉,部署到生產環境時再把注釋取消 // $this->server->set([ // 'daemonize' => true, // ]); // 設置回調函數 $this->server->on('Open', [$this, 'onOpen']); $this->server->on('Message', [$this, 'onMessage']); $this->server->on('Close', [$this, 'onClose']); $this->server->start(); // $output->writeln("WebSocket: Start.\n"); } // 建立連接時回調函數 public function onOpen(\swoole_websocket_server $server, \swoole_http_request $request) { echo "server: handshake success with fd{$request->fd}\n"; } // 收到數據時回調函數 public function onMessage(\swoole_websocket_server $server, \swoole_websocket_frame $frame) { echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n"; $server->push($frame->fd, "this is server"); } // 連接關閉時回調函數 public function onClose($server, $fd) { echo "client {$fd} closed\n"; } } ``` 在命令行輸入 ~~~ php think websocket:start ~~~ 啟動 web socket 服務器 接下來我們使用 JavaScript 代碼來測試 Web Socket 服務器是否正常運行,推薦使用 Chrome 來進行調試 ~~~ <script> var websocket = new WebSocket('ws://localhost:10000'); websocket.onopen = function (evt) { onOpen(evt) }; websocket.onclose = function (evt) { onClose(evt) }; websocket.onmessage = function (evt) { onMessage(evt) }; websocket.onerror = function (evt) { onError(evt) }; function onOpen(evt) { console.log("Connected to WebSocket server."); } function onClose(evt) { console.log("Disconnected"); } function onMessage(evt) { console.log('Retrieved data from server: ' + evt.data); } function onError(evt) { console.log('Error occured: ' + evt.data); } </script> ~~~ ![](https://box.kancloud.cn/dc0198372b19d3182132461cf250f9a7_489x290.png) 打開 Chrome 瀏覽器的調試工具,切換到 Console 選項卡下看到如下圖結果,表示 web socket 連接成功 同時服務器端命令行下應該看到如下回執消息 `server: handshake success with fd1` ![](https://box.kancloud.cn/718dd7204a63c22681994468a2d62644_698x168.png)
                  <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>

                              哎呀哎呀视频在线观看