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

                ## 自定義進程執行異步任務 ### 自定義進程 #### 1、`AsyncTcpTask.php` ```php <?php /** * @desc AsyncTcpTask * @author Tinywan(ShaoBo Wan) * @email 756684177@qq.com * @date 2022/2/26 23:35 */ declare(strict_types=1); namespace process\async; use GuzzleHttp\Client; use GuzzleHttp\Exception\GuzzleException; use Workerman\Connection\TcpConnection; class AsyncTcpTask { /** * @param TcpConnection $connection * @param $data * @throws GuzzleException * @throws \Exception */ public function onMessage(TcpConnection $connection, $data) { try { $formatData = json_decode($data, true,512,JSON_THROW_ON_ERROR); $url = $formatData['data']['url']; $client = new Client(); $response = $client->get($url); $content = $response->getBody()->getContents(); $connection->send($content); } catch (\JsonException $e) { throw new \Exception('Data decoding error'); } } } ``` #### (2)`config/process.php` 配置進程 ```php // 異步任務執行 'async.tcp.task' => [ 'handler' => \process\async\AsyncTcpTask::class, 'listen' => 'text://0.0.0.0:8789', 'count' => 10 ], ``` ### 模擬請求服務端 ```php /** * @param Request $request * @return Response */ public function asyncTcpSleep(Request $request): Response { sleep(3); return response('sleep 1, id:' . $request->get('id')); } ``` > 訪問地址:http://127.0.0.1:8787/test/async-tcp-sleep?id=2002 ### 用自定義協議 ```php $urlBase = 'http://127.0.0.1:8787/test/async-tcp-sleep?id='; // 異步建立一個自定義協議服務器的連接 $textConnection = new AsyncTcpConnection('text://127.0.0.1:8789'); // 當客戶端與Workerman建立連接時(TCP三次握手完成后)觸發的回調函數。每個連接只會觸發一次onConnect回調。 $textConnection->onConnect = function (AsyncTcpConnection $connection) use ($urlBase){ for ($i = 1; $i <= 20; $i++) { $connection->send(json_encode([ 'cmd' => 'get', 'data' => [ 'url' => $urlBase . $i ] ])); } }; // 當客戶端通過連接發來數據時(Workerman收到數據時)觸發的回調函數,即:請求響應的數據包 $textConnection->onMessage = function (AsyncTcpConnection $connection, $result) { Log::info('result2022 = ' . $result.', connection_id = '.$connection->id); }; // 當客戶端連接與Workerman斷開時觸發的回調函數。 $textConnection->onClose = function(AsyncTcpConnection $connection) { // 如果連接斷開,則在3秒后重連 $connection->reConnect(3); }; // 執行異步連接操作。此方法會立刻返回。 $textConnection->connect(); ```
                  <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>

                              哎呀哎呀视频在线观看