{"id":23407,"date":"2024-11-21T14:27:43","date_gmt":"2024-11-21T06:27:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23407\/"},"modified":"2024-11-21T14:27:43","modified_gmt":"2024-11-21T06:27:43","slug":"%e4%bb%8e%e5%9f%ba%e7%a1%80%e5%88%b0%e5%ae%9e%e8%b7%b5%ef%bc%8c%e6%89%8b%e6%8a%8a%e6%89%8b%e6%95%99%e4%bd%a0%e5%ad%a6%e4%b9%a0swoole","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23407\/","title":{"rendered":"\u4ece\u57fa\u7840\u5230\u5b9e\u8df5\uff0c\u624b\u628a\u624b\u6559\u4f60\u5b66\u4e60Swoole"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u65f6\u4ee3\u7684\u4e0d\u65ad\u53d1\u5c55\u548c\u8fdb\u6b65\uff0cweb\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u5df2\u6210\u4e3a\u4f01\u4e1a\u53d1\u5c55\u7684\u5173\u952e\u4e4b\u4e00\u3002\u7136\u800c\uff0c\u4f20\u7edf\u7684php\u5728\u9ad8\u5e76\u53d1\u3001\u5927\u6d41\u91cf\u7684\u60c5\u51b5\u4e0b\u7ecf\u5e38\u5bb9\u6613\u5d29\u6e83\uff0c\u5f71\u54cd\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5e94\u8fd0\u800c\u751f\u3002<\/p>\n<p>Swoole\u662f\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684PHP\u534f\u7a0b\u6846\u67b6\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u5e76\u53d1\u3001\u5f02\u6b65\u7f16\u7a0b\uff0c\u5927\u5927\u63d0\u5347Web\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u6548\u7387\u3002\u672c\u6587\u5c06\u624b\u628a\u624b\u6559\u4f60\u4ece\u57fa\u7840\u5230\u5b9e\u8df5\u5b66\u4e60Swoole\u3002<\/p>\n<p>\u4e00\u3001Swoole\u7684\u57fa\u7840\u77e5\u8bc6<\/p>\n<ol>\n<li>\u534f\u7a0b\u6982\u5ff5<\/li>\n<\/ol>\n<p>\u534f\u7a0b\u662f\u4e00\u79cd\u7528\u6237\u6001\u7684\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u4e0d\u540c\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u7ebf\u7a0b\u3002\u5b83\u53ea\u9700\u8981\u5207\u6362\u4e0a\u4e0b\u6587\u800c\u4e0d\u9700\u8981\u8fdb\u884c\u7cfb\u7edf\u8c03\u7528\uff0c\u5207\u6362\u7684\u901f\u5ea6\u6bd4\u8f83\u5feb\uff0c\u5360\u7528\u5185\u5b58\u4e5f\u6bd4\u8f83\u5c0f\u3002\u534f\u7a0b\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u80fd\u3002<\/p>\n<ol>\n<li>Swoole\u7684\u7279\u70b9<\/li>\n<\/ol>\n<ul>\n<li>\u9ad8\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u3002<\/li>\n<li>\u652f\u6301\u5f02\u6b65IO\u64cd\u4f5c\u3002<\/li>\n<li>\u63d0\u4f9b\u4e86\u5b8c\u5584\u7684\u7f51\u7edc\u901a\u4fe1\u534f\u8bae\uff0c\u5982HTTP\u3001WebSocket\u3001TCP\u3001UDP\u7b49\u3002<\/li>\n<li>\u652f\u6301\u534f\u7a0b\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\u3002<\/li>\n<li>\u63d0\u4f9b\u4e86\u591a\u8fdb\u7a0b\u3001\u591a\u7ebf\u7a0b\u6a21\u578b\uff0c\u53ef\u4ee5\u5145\u5206\u5229\u7528\u591a\u6838CPU\u3002<\/li>\n<\/ul>\n<p>\u4e8c\u3001Swoole\u7684\u5b89\u88c5<\/p>\n<ol>\n<li>\u73af\u5883\u8981\u6c42<\/li>\n<\/ol>\n<ul>\n<li>PHP\u7248\u672c\u5927\u4e8e7.0\u3002<\/li>\n<li>Linux\u3001Unix\u6216MacOS\u64cd\u4f5c\u7cfb\u7edf\u3002<\/li>\n<\/ul>\n<ol>\n<li>\u5b89\u88c5\u547d\u4ee4<\/li>\n<\/ol>\n<p>\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\u5b89\u88c5Swoole\u6269\u5c55\uff1a<\/p>\n<pre>pecl install swoole<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728php.ini\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>extension=swoole.so<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4fdd\u5b58\u5e76\u91cd\u542fPHP-FPM\u3002<\/p>\n<p>\u4e09\u3001Swoole\u7684\u4f7f\u7528<\/p>\n<ol>\n<li>\u7b80\u5355\u6848\u4f8b<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Swoole\u6848\u4f8b\uff0c\u53ef\u4ee5\u8f93\u51faHello, Swoole!\uff1a<\/p>\n<pre>&lt;?php $http = new swoole_http_server(\"127.0.0.1\", 9501);\n\n$http-&gt;on(\"start\", function ($server) {\n    echo \"Swoole http server is started at http:\/\/127.0.0.1:9501\n\";\n});\n\n$http-&amp;gt;on(\"request\", function ($request, $response) {\n    $response-&amp;gt;header(\"Content-Type\", \"text\/plain\");\n    $response-&amp;gt;end(\"Hello, Swoole!\");\n});\n\n$http-&amp;gt;start();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884cphp \u6587\u4ef6\u540d\u547d\u4ee4\uff0c\u6253\u5f00\u6d4f\u89c8\u5668\u8bbf\u95ee http:\/\/localhost:9501\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u8f93\u51fa\u4e86Hello, Swoole! \u3002<\/p>\n<ol>\n<li>WebSocket\u6848\u4f8b<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Swoole WebSocket\u6848\u4f8b\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7b80\u5355\u7684\u804a\u5929\u5ba4\u529f\u80fd\uff1a<\/p>\n<pre>&lt;?php $serv = new swoole_websocket_server(\"127.0.0.1\", 9502);\n\n$serv-&gt;on('open', function ($server, $req) {\n    echo \"connection open: {$req-&amp;gt;fd}\n\";\n});\n\n$serv-&amp;gt;on('message', function ($server, $frame) {\n    echo \"received message: {$frame-&amp;gt;data}\n\";\n    $server-&amp;gt;push($frame-&amp;gt;fd, json_encode([\"hello\", \"world\"]));\n});\n\n$serv-&amp;gt;on('close', function ($server, $fd) {\n    echo \"connection close: {$fd}\n\";\n});\n\n$serv-&amp;gt;start();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884cphp \u6587\u4ef6\u540d\u547d\u4ee4\uff0c\u6253\u5f00\u6d4f\u89c8\u5668\u63a7\u5236\u53f0\uff0c\u6267\u884c\u4ee5\u4e0bJS\u4ee3\u7801\uff1a<\/p>\n<pre>var ws = new WebSocket('ws:\/\/127.0.0.1:9502');\n\nws.onopen = function()\n{\n    console.log('WebSocket is connected');\n};\n\nws.onmessage = function (evt) \n{ \n    console.log('received message: ' + evt.data);\n};\n\nws.onclose = function()\n{ \n    console.log('WebSocket is closed'); \n};\n\nws.send('Hello Swoole WebSocket');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd0\u884c\u540e\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u63a7\u5236\u53f0\u8f93\u51fa\u4e86WebSocket is connected\u548creceived message: [&#8220;hello&#8221;,&#8221;world&#8221;]\u3002<\/p>\n<p>\u56db\u3001\u603b\u7ed3<\/p>\n<p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u5927\u5bb6\u5df2\u7ecf\u638c\u63e1\u4e86Swoole\u534f\u7a0b\u6846\u67b6\u7684\u57fa\u672c\u77e5\u8bc6\u548c\u4f7f\u7528\u65b9\u6cd5\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5145\u5206\u5229\u7528Swoole\u7684\u9ad8\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u548c\u5f02\u6b65\u7f16\u7a0b\u7b49\u7279\u70b9\uff0c\u6765\u63d0\u5347Web\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u6548\u7387\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4ece\u57fa\u7840\u5230\u5b9e\u8df5\uff0c\u624b\u628a\u624b\u6559\u4f60\u5b66\u4e60Swoole\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\u65f6\u4ee3\u7684\u4e0d\u65ad\u53d1\u5c55\u548c\u8fdb\u6b65\uff0cweb\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u5df2\u6210\u4e3a\u4f01\u4e1a\u53d1\u5c55\u7684\u5173\u952e\u4e4b\u4e00\u3002\u7136\u800c\uff0c\u4f20\u7edf\u7684php\u5728\u9ad8\u5e76\u53d1\u3001\u5927\u6d41\u91cf\u7684\u60c5\u51b5\u4e0b\u7ecf\u5e38\u5bb9\u6613\u5d29\u6e83\uff0c\u5f71\u54cd\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5e94\u8fd0\u800c\u751f\u3002 Swoole\u662f\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684PHP\u534f\u7a0b\u6846\u67b6\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u5e76\u53d1\u3001\u5f02\u6b65\u7f16\u7a0b\uff0c\u5927\u5927\u63d0\u5347Web\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u6548\u7387\u3002\u672c\u6587\u5c06\u624b\u628a\u624b\u6559\u4f60\u4ece\u57fa\u7840\u5230\u5b9e\u8df5\u5b66\u4e60Swoole\u3002 \u4e00\u3001Swoole\u7684\u57fa\u7840\u77e5\u8bc6 \u534f\u7a0b\u6982\u5ff5 \u534f\u7a0b\u662f\u4e00\u79cd\u7528\u6237\u6001\u7684\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u4e0d\u540c\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u7ebf\u7a0b\u3002\u5b83\u53ea\u9700\u8981\u5207\u6362\u4e0a\u4e0b\u6587\u800c\u4e0d\u9700\u8981\u8fdb\u884c\u7cfb\u7edf\u8c03\u7528\uff0c\u5207\u6362\u7684\u901f\u5ea6\u6bd4\u8f83\u5feb\uff0c\u5360\u7528\u5185\u5b58\u4e5f\u6bd4\u8f83\u5c0f\u3002\u534f\u7a0b\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u80fd\u3002 Swoole\u7684\u7279\u70b9 \u9ad8\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u3002 \u652f\u6301\u5f02\u6b65IO\u64cd\u4f5c\u3002 \u63d0\u4f9b\u4e86\u5b8c\u5584\u7684\u7f51\u7edc\u901a\u4fe1\u534f\u8bae\uff0c\u5982HTTP\u3001WebSocket\u3001TCP\u3001UDP\u7b49\u3002 \u652f\u6301\u534f\u7a0b\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\u3002 \u63d0\u4f9b\u4e86\u591a\u8fdb\u7a0b\u3001\u591a\u7ebf\u7a0b\u6a21\u578b\uff0c\u53ef\u4ee5\u5145\u5206\u5229\u7528\u591a\u6838CPU\u3002 \u4e8c\u3001Swoole\u7684\u5b89\u88c5 \u73af\u5883\u8981\u6c42 PHP\u7248\u672c\u5927\u4e8e7.0\u3002 Linux\u3001Unix\u6216MacOS\u64cd\u4f5c\u7cfb\u7edf\u3002 \u5b89\u88c5\u547d\u4ee4 \u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\u5b89\u88c5Swoole\u6269\u5c55\uff1a pecl install swoole \u767b\u5f55\u540e\u590d\u5236 \u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728php.ini\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a extension=swoole.so \u767b\u5f55\u540e\u590d\u5236 \u4fdd\u5b58\u5e76\u91cd\u542fPHP-FPM\u3002 \u4e09\u3001Swoole\u7684\u4f7f\u7528 \u7b80\u5355\u6848\u4f8b \u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Swoole\u6848\u4f8b\uff0c\u53ef\u4ee5\u8f93\u51faHello, Swoole!\uff1a &lt;?php $http = new swoole_http_server(&#8220;127.0.0.1&#8221;, 9501); $http-&gt;on(&#8220;start&#8221;, function ($server) { echo &#8220;Swoole http server is started at http:\/\/127.0.0.1:9501 &#8220;; }); $http-&amp;gt;on(&#8220;request&#8221;, function ($request, $response) { $response-&amp;gt;header(&#8220;Content-Type&#8221;, [&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-23407","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23407","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=23407"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23407\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}