{"id":24985,"date":"2024-11-21T14:20:57","date_gmt":"2024-11-21T06:20:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24985\/"},"modified":"2024-11-21T14:20:57","modified_gmt":"2024-11-21T06:20:57","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8swoole%e5%bc%80%e5%8f%91%e9%ab%98%e6%80%a7%e8%83%bd%e7%9a%84%e7%bd%91%e7%bb%9c%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24985\/","title":{"rendered":"\u5982\u4f55\u5229\u7528Swoole\u5f00\u53d1\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u670d\u52a1\u5668"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169933872393011.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5229\u7528Swoole\u5f00\u53d1\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u670d\u52a1\u5668\u63d2\u56fe\" alt=\"\u5982\u4f55\u5229\u7528Swoole\u5f00\u53d1\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u670d\u52a1\u5668\u63d2\u56fe\" \/><\/p>\n<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u53d1\u5c55\uff0c\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u670d\u52a1\u5668\u8d8a\u6765\u8d8a\u53d7\u5230\u5f00\u53d1\u8005\u7684\u9752\u7750\u3002Swoole\u4f5c\u4e3a\u4e00\u6b3e\u57fa\u4e8ePHP\u8bed\u8a00\u5f00\u53d1\u7684\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u5f15\u64ce\uff0c\u80fd\u591f\u5927\u5e45\u5ea6\u63d0\u5347\u7f51\u7edc\u901a\u4fe1\u7684\u6548\u7387\uff0c\u5728\u4e92\u8054\u7f51\u5e94\u7528\u9886\u57df\u53d7\u5230\u4e86\u5e7f\u6cdb\u7684\u5e94\u7528\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u5982\u4f55\u5229\u7528Swoole\u6765\u5f00\u53d1\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u670d\u52a1\u5668\u5462\uff1f\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u5e26\u5927\u5bb6\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u6df1\u5165\u4e86\u89e3Swoole\u7684\u5f00\u53d1\u548c\u5e94\u7528\u3002<\/p>\n<p>\u4e00\u3001\u5b89\u88c5Swoole<\/p>\n<p>\u5b89\u88c5Swoole\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528PHP\u5b98\u65b9\u6269\u5c55\u7ba1\u7406\u5668PECL\u5373\u53ef\u3002\u5177\u4f53\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>$ pecl install swoole<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u7f16\u8f91php.ini\u6587\u4ef6\uff0c\u5728\u5176\u4e2d\u589e\u52a0\u4e00\u884c\u914d\u7f6e\uff0c\u542f\u7528\u6269\u5c55\u5373\u53ef\u3002<\/p>\n<pre>extension=swoole.so<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e8c\u3001\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684HTTP\u670d\u52a1\u5668<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684HTTP\u670d\u52a1\u5668\uff0c\u4f9b\u5927\u5bb6\u4e86\u89e3Swoole\u7684\u57fa\u672c\u7528\u6cd5\u3002<\/p>\n<pre>&lt;?php $http = new swoole_http_server(\"127.0.0.1\", 9501);\n\n$http-&gt;on('request', function ($request, $response) {\n    $response-&amp;gt;header(\"Content-Type\", \"text\/html; charset=utf-8\");\n    $response-&amp;gt;end(\"&lt;h1&gt;Hello Swoole!&lt;\/h1&gt;\");\n});\n\n$http-&amp;gt;start();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u542f\u52a8HTTP\u670d\u52a1\u5668\uff1a<\/p>\n<pre>$ php server.php<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\uff0c\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee http:\/\/127.0.0.1:9501 \u5373\u53ef\u770b\u5230\u8f93\u51fa\u7684 Hello Swoole!\u3002<\/p>\n<p>\u4e09\u3001\u5229\u7528WebSocket\u5b9e\u73b0\u5b9e\u65f6\u901a\u4fe1<\/p>\n<p>Swoole\u8fd8\u652f\u6301\u4f7f\u7528WebSocket\u534f\u8bae\u5b9e\u73b0\u5b9e\u65f6\u901a\u4fe1\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u804a\u5929\u5ba4\u5e94\u7528\uff0c\u4f9b\u5927\u5bb6\u4e86\u89e3Swoole\u7684\u5b9e\u65f6\u901a\u4fe1\u529f\u80fd\u3002<\/p>\n<pre>&lt;?php $ws = new swoole_websocket_server(\"127.0.0.1\", 9502);\n\n$ws-&gt;on('open', function ($ws, $request) {\n    echo \"client-{$request-&amp;gt;fd} is connected\n\";\n});\n\n$ws-&amp;gt;on('message', function ($ws, $frame) {\n    echo \"received message: {$frame-&amp;gt;data}\n\";\n    foreach($ws-&amp;gt;connections as $fd) {\n        $ws-&amp;gt;push($fd, $frame-&amp;gt;data);\n    }\n});\n\n$ws-&amp;gt;on('close', function ($ws, $fd) {\n    echo \"client-{$fd} is closed\n\";\n});\n\n$ws-&amp;gt;start();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u542f\u52a8WebSocket\u670d\u52a1\u5668\uff1a<\/p>\n<pre>$ php chatroom.php<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\uff0c\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee http:\/\/localhost:8080 \uff0c\u6253\u5f00JavaScript\u63a7\u5236\u53f0\uff0c\u6267\u884c\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>let ws = new WebSocket('ws:\/\/127.0.0.1:9502');\n\nws.onopen = function() {\n    console.log('WebSocket is connected.');\n};\n\nws.onmessage = function(event) {\n    console.log(`received message: ${event.data}`);\n};\n\nws.onclose = function() {\n    console.log('WebSocket is closed.');\n};\n\nws.send('hello world');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u65f6\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728JavaScript\u63a7\u5236\u53f0\u4e2d\u770b\u5230\u6536\u5230\u4e86\u670d\u52a1\u7aef\u53d1\u9001\u7684\u6d88\u606f\u3002\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00\u591a\u4e2a\u6807\u7b7e\u9875\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u770b\u5230\u4e00\u4e2a\u7b80\u5355\u7684\u804a\u5929\u5ba4\u6548\u679c\u3002<\/p>\n<p>\u56db\u3001\u5229\u7528\u534f\u7a0b\u63d0\u9ad8\u5e76\u53d1\u80fd\u529b<\/p>\n<p>Swoole\u652f\u6301\u5229\u7528\u534f\u7a0b\u63d0\u9ad8\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u534f\u7a0b\u5e94\u7528\uff0c\u4f9b\u5927\u5bb6\u4e86\u89e3Swoole\u7684\u534f\u7a0b\u529f\u80fd\u3002<\/p>\n<pre>&lt;?php Coun(function() {\n    $result1 = Co::exec('ls');\n    $result2 = Co::exec('pwd');\n    $result3 = Co::exec('date');\n    \n    echo \"result1: {$result1[0]}\n\";\n    echo \"result2: {$result2[0]}\n\";\n    echo \"result3: {$result3[0]}\n\";\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u8fd0\u884c\u534f\u7a0b\u5e94\u7528\uff1a<\/p>\n<pre>$ php coroutine.php<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u6267\u884c\u7ed3\u679c\uff0c\u5206\u522b\u5bf9\u5e94\u4e86\u6267\u884c ls\u3001pwd \u548c date \u547d\u4ee4\u7684\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<p>\u4ee5\u4e0a\u662fSwoole\u7684\u57fa\u672c\u5e94\u7528\u793a\u4f8b\uff0c\u6211\u4eec\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\u4ecb\u7ecd\u4e86Swoole\u7684\u57fa\u7840\u77e5\u8bc6\u70b9\uff0c\u5305\u62ecHTTP\u3001WebSocket\u3001\u534f\u7a0b\u7b49\u3002\u76f8\u4fe1\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\uff0c\u5927\u5bb6\u5df2\u7ecf\u53ef\u4ee5\u638c\u63e1Swoole\u7684\u57fa\u672c\u7528\u6cd5\uff0c\u5f00\u53d1\u51fa\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u670d\u52a1\u5668\u4e86\u3002\u5f53\u7136\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u8fd8\u9700\u8981\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u66f4\u590d\u6742\u7684\u5e94\u7528\u573a\u666f\uff0c\u9700\u8981\u5927\u5bb6\u4e0d\u65ad\u5b66\u4e60\u548c\u63a2\u7d22\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5229\u7528Swoole\u5f00\u53d1\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u670d\u52a1\u5668\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u53d1\u5c55\uff0c\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u670d\u52a1\u5668\u8d8a\u6765\u8d8a\u53d7\u5230\u5f00\u53d1\u8005\u7684\u9752\u7750\u3002Swoole\u4f5c\u4e3a\u4e00\u6b3e\u57fa\u4e8ePHP\u8bed\u8a00\u5f00\u53d1\u7684\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u5f15\u64ce\uff0c\u80fd\u591f\u5927\u5e45\u5ea6\u63d0\u5347\u7f51\u7edc\u901a\u4fe1\u7684\u6548\u7387\uff0c\u5728\u4e92\u8054\u7f51\u5e94\u7528\u9886\u57df\u53d7\u5230\u4e86\u5e7f\u6cdb\u7684\u5e94\u7528\u3002 \u90a3\u4e48\uff0c\u5982\u4f55\u5229\u7528Swoole\u6765\u5f00\u53d1\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u670d\u52a1\u5668\u5462\uff1f\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u5e26\u5927\u5bb6\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u6df1\u5165\u4e86\u89e3Swoole\u7684\u5f00\u53d1\u548c\u5e94\u7528\u3002 \u4e00\u3001\u5b89\u88c5Swoole \u5b89\u88c5Swoole\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528PHP\u5b98\u65b9\u6269\u5c55\u7ba1\u7406\u5668PECL\u5373\u53ef\u3002\u5177\u4f53\u65b9\u6cd5\u5982\u4e0b\uff1a $ pecl install swoole \u767b\u5f55\u540e\u590d\u5236 \u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u7f16\u8f91php.ini\u6587\u4ef6\uff0c\u5728\u5176\u4e2d\u589e\u52a0\u4e00\u884c\u914d\u7f6e\uff0c\u542f\u7528\u6269\u5c55\u5373\u53ef\u3002 extension=swoole.so \u767b\u5f55\u540e\u590d\u5236 \u4e8c\u3001\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684HTTP\u670d\u52a1\u5668 \u4e0b\u9762\u6211\u4eec\u6765\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684HTTP\u670d\u52a1\u5668\uff0c\u4f9b\u5927\u5bb6\u4e86\u89e3Swoole\u7684\u57fa\u672c\u7528\u6cd5\u3002 &lt;?php $http = new swoole_http_server(&#8220;127.0.0.1&#8221;, 9501); $http-&gt;on(&#8216;request&#8217;, function ($request, $response) { $response-&amp;gt;header(&#8220;Content-Type&#8221;, &#8220;text\/html; charset=utf-8&#8221;); $response-&amp;gt;end(&#8220;&lt;h1&gt;Hello Swoole!&lt;\/h1&gt;&#8221;); }); $http-&amp;gt;start(); \u767b\u5f55\u540e\u590d\u5236 \u6211\u4eec\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u542f\u52a8HTTP\u670d\u52a1\u5668\uff1a $ php server.php \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\uff0c\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee http:\/\/127.0.0.1:9501 \u5373\u53ef\u770b\u5230\u8f93\u51fa\u7684 Hello Swoole!\u3002 \u4e09\u3001\u5229\u7528WebSocket\u5b9e\u73b0\u5b9e\u65f6\u901a\u4fe1 Swoole\u8fd8\u652f\u6301\u4f7f\u7528WebSocket\u534f\u8bae\u5b9e\u73b0\u5b9e\u65f6\u901a\u4fe1\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u804a\u5929\u5ba4\u5e94\u7528\uff0c\u4f9b\u5927\u5bb6\u4e86\u89e3Swoole\u7684\u5b9e\u65f6\u901a\u4fe1\u529f\u80fd\u3002 &lt;?php $ws = new swoole_websocket_server(&#8220;127.0.0.1&#8221;, 9502); $ws-&gt;on(&#8216;open&#8217;, function ($ws, $request) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24985","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24985","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24985"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24985\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}