{"id":1068,"date":"2025-01-19T13:48:29","date_gmt":"2025-01-19T05:48:29","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1068"},"modified":"2025-01-19T13:48:29","modified_gmt":"2025-01-19T05:48:29","slug":"swoole-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e5%85%a5%e9%97%a8%e4%b8%8e%e9%ab%98%e7%ba%a7%e6%8a%80%e5%b7%a7%ef%bc%9a%e5%bf%ab%e9%80%9f%e6%9e%84%e5%bb%ba%e9%ab%98%e5%b9%b6%e5%8f%91%e7%bd%91%e7%bb%9c","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/19\/swoole-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e5%85%a5%e9%97%a8%e4%b8%8e%e9%ab%98%e7%ba%a7%e6%8a%80%e5%b7%a7%ef%bc%9a%e5%bf%ab%e9%80%9f%e6%9e%84%e5%bb%ba%e9%ab%98%e5%b9%b6%e5%8f%91%e7%bd%91%e7%bb%9c\/","title":{"rendered":"Swoole \u7f51\u7edc\u7f16\u7a0b\u5165\u95e8\u4e0e\u9ad8\u7ea7\u6280\u5de7\uff1a\u5feb\u901f\u6784\u5efa\u9ad8\u5e76\u53d1\u7f51\u7edc\u5e94\u7528"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">Swoole \u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684 PHP \u6269\u5c55\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7f51\u7edc\u7f16\u7a0b\u529f\u80fd\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u6784\u5efa\u9ad8\u6548\u7684\u5e76\u53d1\u7f51\u7edc\u5e94\u7528\u3002\u901a\u8fc7 Swoole \u63d0\u4f9b\u7684\u591a\u8fdb\u7a0b\u3001\u534f\u7a0b\u548c\u5f02\u6b65 I\/O \u7279\u6027\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u9ad8\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u670d\u52a1\uff0c\u5305\u62ec Web \u670d\u52a1\u5668\u3001TCP\/UDP \u670d\u52a1\u5668\u3001WebSocket \u670d\u52a1\u7b49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd Swoole \u7f51\u7edc\u7f16\u7a0b\u7684\u6838\u5fc3\u6982\u5ff5\u3001\u529f\u80fd\u548c\u5b9e\u9645\u5e94\u7528\uff0c\u5305\u62ec <strong>TCP\/UDP \u670d\u52a1<\/strong>\u3001<strong>WebSocket \u670d\u52a1<\/strong>\u3001<strong>HTTP \u670d\u52a1<\/strong> \u7b49\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>Swoole \u7f51\u7edc\u7f16\u7a0b\u57fa\u7840<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u63d0\u4f9b\u4e86\u591a\u79cd\u7f51\u7edc\u7f16\u7a0b\u534f\u8bae\u548c\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u6784\u5efa\u9ad8\u6548\u7684\u7f51\u7edc\u670d\u52a1\u3002Swoole \u652f\u6301 <strong>TCP<\/strong>\u3001<strong>UDP<\/strong>\u3001<strong>Unix Socket<\/strong>\u3001<strong>WebSocket<\/strong> \u548c <strong>HTTP<\/strong> \u7b49\u534f\u8bae\u7684\u5f00\u53d1\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1.1 <strong>Swoole \u7f51\u7edc\u7f16\u7a0b\u6a21\u578b<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u57fa\u4e8e\u4e8b\u4ef6\u9a71\u52a8\u548c\u5f02\u6b65\u975e\u963b\u585e I\/O \u6a21\u578b\uff0c\u80fd\u591f\u5904\u7406\u5927\u91cf\u5e76\u53d1\u7684\u7f51\u7edc\u8bf7\u6c42\u3002\u5176\u4e3b\u8981\u7279\u70b9\u662f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e8b\u4ef6\u9a71\u52a8<\/strong>\uff1a\u901a\u8fc7\u4e8b\u4ef6\u5faa\u73af\u673a\u5236\uff0cSwoole \u5904\u7406\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\u3001\u6570\u636e\u6536\u53d1\u3001\u8fde\u63a5\u5173\u95ed\u7b49\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u5f02\u6b65\u975e\u963b\u585e I\/O<\/strong>\uff1a\u7f51\u7edc\u8bfb\u5199\u64cd\u4f5c\u4e0d\u963b\u585e\u5f53\u524d\u8fdb\u7a0b\u6216\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>TCP\/UDP \u670d\u52a1<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 <strong>TCP \u670d\u52a1\u5668<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u63d0\u4f9b\u4e86\u975e\u5e38\u7b80\u6d01\u7684 API \u6765\u521b\u5efa\u548c\u7ba1\u7406 TCP \u670d\u52a1\u5668\uff0c\u652f\u6301\u591a\u8fdb\u7a0b\u548c\u534f\u7a0b\u6a21\u5f0f\uff0c\u80fd\u591f\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">2.1.1 <strong>\u521b\u5efa TCP \u670d\u52a1\u5668<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 <code>Swoole\\Server<\/code> \u7c7b\u6765\u521b\u5efa\u4e00\u4e2a TCP \u670d\u52a1\u5668\uff0c\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u76d1\u542c\u7684 IP \u5730\u5740\u548c\u7aef\u53e3\u6765\u542f\u52a8\u670d\u52a1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 TCP \u670d\u52a1\u5668\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u521b\u5efa\u4e00\u4e2a TCP \u670d\u52a1\u5668\n$server = new Swoole\\Server(\"127.0.0.1\", 9501);\n\n\/\/ \u8bbe\u7f6e\u670d\u52a1\u5668\u914d\u7f6e\n$server-&gt;set(&#91;\n    'worker_num' =&gt; 4,  \/\/ \u8bbe\u7f6e\u5de5\u4f5c\u8fdb\u7a0b\u6570\u91cf\n    'daemonize' =&gt; false,  \/\/ \u4e0d\u5b88\u62a4\u5316\uff0c\u76f4\u63a5\u5728\u7ec8\u7aef\u8fd0\u884c\n]);\n\n\/\/ \u6ce8\u518c\u8fde\u63a5\u4e8b\u4ef6\n$server-&gt;on(\"Connect\", function ($server, $fd) {\n    echo \"Client {$fd} connected\\n\";\n});\n\n\/\/ \u6ce8\u518c\u63a5\u6536\u6570\u636e\u4e8b\u4ef6\n$server-&gt;on(\"Receive\", function ($server, $fd, $reactorId, $data) {\n    echo \"Received data from client {$fd}: {$data}\\n\";\n    $server-&gt;send($fd, \"Hello, client!\");  \/\/ \u5411\u5ba2\u6237\u7aef\u53d1\u9001\u6570\u636e\n});\n\n\/\/ \u6ce8\u518c\u5173\u95ed\u8fde\u63a5\u4e8b\u4ef6\n$server-&gt;on(\"Close\", function ($server, $fd) {\n    echo \"Client {$fd} disconnected\\n\";\n});\n\n\/\/ \u542f\u52a8\u670d\u52a1\u5668\n$server-&gt;start();\n?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Swoole\\Server<\/code> \u7528\u4e8e\u521b\u5efa\u4e00\u4e2a TCP \u670d\u52a1\u5668\uff0c\u76d1\u542c <code>127.0.0.1:9501<\/code>\u3002<\/li>\n\n\n\n<li><code>on(\"Connect\")<\/code> \u4e8b\u4ef6\u5904\u7406\u8fde\u63a5\u7684\u5efa\u7acb\u3002<\/li>\n\n\n\n<li><code>on(\"Receive\")<\/code> \u4e8b\u4ef6\u5904\u7406\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e\u3002<\/li>\n\n\n\n<li><code>on(\"Close\")<\/code> \u4e8b\u4ef6\u5904\u7406\u8fde\u63a5\u5173\u95ed\u3002<\/li>\n\n\n\n<li><code>server->send()<\/code> \u7528\u4e8e\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">2.1.2 <strong>\u542f\u52a8\u591a\u4e2a\u8fdb\u7a0b<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u8bbe\u7f6e <code>worker_num<\/code>\uff0cSwoole \u53ef\u4ee5\u542f\u52a8\u591a\u4e2a\u5de5\u4f5c\u8fdb\u7a0b\u6765\u5904\u7406\u4e0d\u540c\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\uff0c\u63d0\u9ad8\u5e76\u53d1\u80fd\u529b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 <strong>UDP \u670d\u52a1\u5668<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">UDP \u662f\u4e00\u79cd\u65e0\u8fde\u63a5\u7684\u534f\u8bae\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u6570\u636e\u4f20\u8f93\u4f46\u4e0d\u8981\u6c42\u53ef\u9760\u4f20\u8f93\u7684\u5e94\u7528\uff08\u5982\u5b9e\u65f6\u901a\u4fe1\u3001\u89c6\u9891\u6d41\u7b49\uff09\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">2.2.1 <strong>\u521b\u5efa UDP \u670d\u52a1\u5668<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u4e5f\u652f\u6301\u521b\u5efa UDP \u670d\u52a1\u5668\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u521b\u5efa\u4e00\u4e2a UDP \u670d\u52a1\u5668\n$server = new Swoole\\Server(\"127.0.0.1\", 9502, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);\n\n\/\/ \u6ce8\u518c\u63a5\u6536\u6570\u636e\u4e8b\u4ef6\n$server-&gt;on(\"Packet\", function ($server, $data, $clientInfo) {\n    echo \"Received UDP packet: {$data}\\n\";\n    $server-&gt;sendto($clientInfo&#91;'address'], $clientInfo&#91;'port'], \"Reply: {$data}\");\n});\n\n\/\/ \u542f\u52a8\u670d\u52a1\u5668\n$server-&gt;start();\n?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Swoole\\Server<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a UDP \u670d\u52a1\u5668\uff0c\u76d1\u542c <code>127.0.0.1:9502<\/code>\u3002<\/li>\n\n\n\n<li><code>on(\"Packet\")<\/code> \u4e8b\u4ef6\u7528\u4e8e\u63a5\u6536\u5ba2\u6237\u7aef\u53d1\u9001\u7684 UDP \u6570\u636e\u5305\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>sendto()<\/code> \u5411\u5ba2\u6237\u7aef\u7684 IP \u5730\u5740\u548c\u7aef\u53e3\u53d1\u9001\u54cd\u5e94\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">UDP \u670d\u52a1\u5668\u4e0d\u9700\u8981\u5efa\u7acb\u8fde\u63a5\uff0c\u6570\u636e\u5305\u76f4\u63a5\u53d1\u9001\u548c\u63a5\u6536\uff0c\u56e0\u6b64\u66f4\u52a0\u9ad8\u6548\uff0c\u4f46\u6ca1\u6709 TCP \u90a3\u6837\u7684\u53ef\u9760\u6027\u4fdd\u8bc1\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>WebSocket \u670d\u52a1<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">WebSocket \u662f\u4e00\u79cd\u5728\u5355\u4e2a TCP \u8fde\u63a5\u4e0a\u8fdb\u884c\u5168\u53cc\u5de5\u901a\u4fe1\u7684\u534f\u8bae\uff0c\u5e38\u7528\u4e8e\u5b9e\u65f6\u5e94\u7528\uff0c\u5982\u804a\u5929\u5e94\u7528\u3001\u5b9e\u65f6\u63a8\u9001\u7b49\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 <strong>\u521b\u5efa WebSocket \u670d\u52a1\u5668<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u63d0\u4f9b\u4e86\u4e13\u95e8\u7684 WebSocket \u670d\u52a1\u5668\u7c7b\uff0c\u652f\u6301 WebSocket \u534f\u8bae\u7684\u8fde\u63a5\u3001\u6570\u636e\u6536\u53d1\u548c\u5e7f\u64ad\u7b49\u529f\u80fd\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 WebSocket \u670d\u52a1\u5668\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u521b\u5efa\u4e00\u4e2a WebSocket \u670d\u52a1\u5668\n$server = new Swoole\\WebSocket\\Server(\"127.0.0.1\", 9503);\n\n\/\/ \u6ce8\u518c\u8fde\u63a5\u4e8b\u4ef6\n$server-&gt;on(\"Open\", function ($server, $request) {\n    echo \"Connection established with fd: {$request-&gt;fd}\\n\";\n});\n\n\/\/ \u6ce8\u518c\u63a5\u6536\u6570\u636e\u4e8b\u4ef6\n$server-&gt;on(\"Message\", function ($server, $frame) {\n    echo \"Received message: {$frame-&gt;data}\\n\";\n    $server-&gt;push($frame-&gt;fd, \"Hello, WebSocket client!\");  \/\/ \u5411\u5ba2\u6237\u7aef\u53d1\u9001\u6d88\u606f\n});\n\n\/\/ \u6ce8\u518c\u5173\u95ed\u8fde\u63a5\u4e8b\u4ef6\n$server-&gt;on(\"Close\", function ($server, $fd) {\n    echo \"Connection closed with fd: {$fd}\\n\";\n});\n\n\/\/ \u542f\u52a8\u670d\u52a1\u5668\n$server-&gt;start();\n?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Swoole\\WebSocket\\Server<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a WebSocket \u670d\u52a1\u5668\uff0c\u76d1\u542c <code>127.0.0.1:9503<\/code>\u3002<\/li>\n\n\n\n<li><code>on(\"Open\")<\/code> \u4e8b\u4ef6\u5904\u7406 WebSocket \u8fde\u63a5\u7684\u5efa\u7acb\u3002<\/li>\n\n\n\n<li><code>on(\"Message\")<\/code> \u4e8b\u4ef6\u5904\u7406\u5ba2\u6237\u7aef\u53d1\u9001\u7684 WebSocket \u6d88\u606f\u3002<\/li>\n\n\n\n<li><code>on(\"Close\")<\/code> \u4e8b\u4ef6\u5904\u7406\u8fde\u63a5\u5173\u95ed\u3002<\/li>\n\n\n\n<li><code>push()<\/code> \u7528\u4e8e\u5411\u5ba2\u6237\u7aef\u53d1\u9001 WebSocket \u6d88\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3.2 <strong>\u5e7f\u64ad\u529f\u80fd<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">WebSocket \u670d\u52a1\u5668\u53ef\u4ee5\u5b9e\u73b0\u5e7f\u64ad\u529f\u80fd\uff0c\u5411\u6240\u6709\u8fde\u63a5\u7684\u5ba2\u6237\u7aef\u53d1\u9001\u6d88\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$server-&gt;on(\"Message\", function ($server, $frame) {\n    echo \"Received message: {$frame-&gt;data}\\n\";\n    \/\/ \u5e7f\u64ad\u6d88\u606f\u7ed9\u6240\u6709\u8fde\u63a5\u7684\u5ba2\u6237\u7aef\n    foreach ($server-&gt;connections as $fd) {\n        if ($fd != $frame-&gt;fd) {\n            $server-&gt;push($fd, \"Broadcast: {$frame-&gt;data}\");\n        }\n    }\n});<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>HTTP \u670d\u52a1<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u4e5f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684 HTTP \u670d\u52a1\u5668\u529f\u80fd\uff0c\u80fd\u591f\u5904\u7406\u9ad8\u5e76\u53d1\u7684 Web \u8bf7\u6c42\u3002Swoole \u652f\u6301\u6807\u51c6\u7684 HTTP \u534f\u8bae\uff0c\u53ef\u4ee5\u4f5c\u4e3a\u4f20\u7edf Web \u670d\u52a1\u5668\u7684\u66ff\u4ee3\u65b9\u6848\uff0c\u6216\u8005\u7528\u4e8e\u6784\u5efa RESTful API\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4.1 <strong>\u521b\u5efa HTTP \u670d\u52a1\u5668<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 <code>Swoole\\Http\\Server<\/code> \u7c7b\u521b\u5efa\u4e00\u4e2a HTTP \u670d\u52a1\u5668\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u521b\u5efa\u4e00\u4e2a HTTP \u670d\u52a1\u5668\n$httpServer = new Swoole\\Http\\Server(\"127.0.0.1\", 9504);\n\n\/\/ \u6ce8\u518c\u8bf7\u6c42\u4e8b\u4ef6\n$httpServer-&gt;on(\"Request\", function ($request, $response) {\n    $response-&gt;header(\"Content-Type\", \"text\/html\");\n    $response-&gt;end(\"&lt;h1&gt;Hello, Swoole HTTP Server!&lt;\/h1&gt;\");\n});\n\n\/\/ \u542f\u52a8\u670d\u52a1\u5668\n$httpServer-&gt;start();\n?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Swoole\\Http\\Server<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a HTTP \u670d\u52a1\u5668\uff0c\u76d1\u542c <code>127.0.0.1:9504<\/code>\u3002<\/li>\n\n\n\n<li><code>on(\"Request\")<\/code> \u4e8b\u4ef6\u5904\u7406\u6bcf\u4e2a HTTP \u8bf7\u6c42\uff0c\u5e76\u8fd4\u56de\u7b80\u5355\u7684 HTML \u9875\u9762\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4.2 <strong>HTTP \u8def\u7531<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u4f7f\u7528 <code>on(\"Request\")<\/code> \u6765\u5904\u7406 HTTP \u8bf7\u6c42\uff0c\u6839\u636e\u4e0d\u540c\u7684 URL \u548c HTTP \u65b9\u6cd5\u8fdb\u884c\u8def\u7531\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$httpServer-&gt;on(\"Request\", function ($request, $response) {\n    if ($request-&gt;server&#91;'request_uri'] == '\/hello') {\n        $response-&gt;end(\"Hello, Swoole!\");\n    } else {\n        $response-&gt;status(404);\n        $response-&gt;end(\"Page not found\");\n    }\n});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6839\u636e\u8bf7\u6c42\u7684 URL \u8def\u5f84\u8fdb\u884c\u4e0d\u540c\u7684\u54cd\u5e94\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u7f51\u7edc\u7f16\u7a0b\u4e3a PHP \u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u591a\u534f\u8bae\u670d\u52a1\u652f\u6301\uff0c\u5305\u62ec TCP\u3001UDP\u3001WebSocket \u548c HTTP \u7b49\u3002\u901a\u8fc7\u8fdb\u7a0b\u6c60\u3001\u534f\u7a0b\u548c\u5f02\u6b65 I\/O\uff0cSwoole \u80fd\u591f\u8f7b\u677e\u5904\u7406\u5927\u89c4\u6a21\u5e76\u53d1\u8fde\u63a5\uff0c\u9002\u5408\u9ad8\u5e76\u53d1\u7684\u5b9e\u65f6\u901a\u4fe1\u3001\u6570\u636e\u4f20\u8f93\u548c API \u670d\u52a1\u7b49\u5e94\u7528\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swoole \u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684 PHP \u6269\u5c55\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7f51\u7edc\u7f16\u7a0b\u529f\u80fd\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u6784\u5efa\u9ad8\u6548\u7684\u5e76\u53d1\u7f51\u7edc\u5e94\u7528\u3002\u901a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64],"tags":[],"class_list":["post-1068","post","type-post","status-publish","format-standard","hentry","category-swoole"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1068"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1068\/revisions"}],"predecessor-version":[{"id":1069,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1068\/revisions\/1069"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}