{"id":24623,"date":"2024-11-21T10:58:40","date_gmt":"2024-11-21T02:58:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24623\/"},"modified":"2024-11-21T10:58:40","modified_gmt":"2024-11-21T02:58:40","slug":"swoole%e5%92%8cworkerman%e6%8a%80%e6%9c%af%e9%80%89%e6%8b%a9%e6%8c%87%e5%8d%97%ef%bc%9a%e5%93%aa%e4%b8%aa%e6%9b%b4%e6%98%93%e5%ad%a6%e4%b9%a0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24623\/","title":{"rendered":"swoole\u548cworkerman\u6280\u672f\u9009\u62e9\u6307\u5357\uff1a\u54ea\u4e2a\u66f4\u6613\u5b66\u4e60\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169424166871863.jpg\" class=\"aligncenter\" title=\"swoole\u548cworkerman\u6280\u672f\u9009\u62e9\u6307\u5357\uff1a\u54ea\u4e2a\u66f4\u6613\u5b66\u4e60\uff1f\u63d2\u56fe\" alt=\"swoole\u548cworkerman\u6280\u672f\u9009\u62e9\u6307\u5357\uff1a\u54ea\u4e2a\u66f4\u6613\u5b66\u4e60\uff1f\u63d2\u56fe\" \/><\/p>\n<p>\u548c\u6280\u672f\u9009\u62e9\u6307\u5357\uff1a\u54ea\u4e2a\u66f4\u6613\u5b66\u4e60\uff1f<\/p>\n<p>\u5f15\u8a00<br \/>\u5728\u5f53\u524d\u7684Web\u5f00\u53d1\u73af\u5883\u4e2d\uff0c\u975e\u963b\u585e\u7684\u670d\u52a1\u5668\u5f00\u53d1\u6280\u672f\u8d8a\u6765\u8d8a\u53d7\u5230\u5f00\u53d1\u8005\u7684\u5173\u6ce8\u3002\u5728\u8fd9\u5176\u4e2d\uff0cswoole\u548cworkerman\u65e0\u7591\u662f\u4e24\u4e2a\u5907\u53d7\u77a9\u76ee\u7684\u6280\u672f\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u6280\u672f\u53ef\u80fd\u4f1a\u662f\u4e00\u4e2a\u6311\u6218\u3002\u672c\u6587\u5c06\u4ece\u5b66\u4e60\u66f2\u7ebf\u7684\u89d2\u5ea6\uff0c\u6bd4\u8f83swoole\u548cworkerman\u7684\u6613\u5b66\u7a0b\u5ea6\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\uff0c\u4ee5\u5e2e\u52a9\u8bfb\u8005\u505a\u51fa\u9009\u62e9\u3002<\/p>\n<p>swoole<br \/>swoole\u662f\u4e00\u6b3e\u57fa\u4e8eC++\u8bed\u8a00\u7f16\u5199\u7684PHP\u6269\u5c55\uff0c\u63d0\u4f9b\u4e86\u4e00\u5957\u5b8c\u5584\u7684API\uff0c\u4f7f\u5f97PHP\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u4e0d\u5207\u6362\u8bed\u8a00\u7684\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7swoole\u9ad8\u6548\u5730\u5f00\u53d1\u975e\u963b\u585e\u7684\u670d\u52a1\u5668\u7a0b\u5e8f\u3002\u901a\u8fc7\u4f7f\u7528swoole\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5145\u5206\u5229\u7528PHP\u7684\u7075\u6d3b\u6027\u548c\u6613\u7528\u6027\uff0c\u540c\u65f6\u4eab\u53d7\u5230\u975e\u963b\u585eIO\u7684\u9ad8\u6027\u80fd\u3002<\/p>\n<p>swoole\u7684\u5b66\u4e60\u66f2\u7ebf\u76f8\u5bf9\u6765\u8bf4\u8f83\u9661\u5ced\u3002\u8981\u5b66\u4e60swoole\uff0c\u9996\u5148\u9700\u8981\u638c\u63e1PHP\u57fa\u7840\u77e5\u8bc6\uff0c\u5305\u62ec\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u3001\u7f51\u7edc\u7f16\u7a0b\u7b49\u3002\u5176\u6b21\uff0c\u9700\u8981\u4e86\u89e3\u4e00\u4e9b\u5e95\u5c42\u7684\u7f51\u7edc\u901a\u4fe1\u539f\u7406\uff0c\u5982TCP\/IP\u534f\u8bae\u6808\u3001\u591a\u7ebf\u7a0b\u3001\u5f02\u6b65IO\u7b49\u3002\u6700\u540e\uff0c\u9700\u8981\u719f\u6089swoole\u63d0\u4f9b\u7684\u5404\u79cdAPI\uff0c\u5982Server\u3001Coroutine\u7b49\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684swoole\u670d\u52a1\u5668\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;?php \/\/ \u521b\u5efa\u4e00\u4e2aHTTP\u670d\u52a1\u5668\n$http = new swoole_http_server(\"127.0.0.1\", 9501);\n\n\/\/ \u76d1\u542c\u8bf7\u6c42\u4e8b\u4ef6\n$http-&gt;on('request', function($request, $response) {\n    \/\/ \u5904\u7406\u8bf7\u6c42\n    $response-&amp;gt;header(\"Content-Type\", \"text\/plain\");\n    $response-&amp;gt;end(\"Hello, Swoole!\");\n});\n\n\/\/ \u542f\u52a8\u670d\u52a1\u5668\n$http-&amp;gt;start();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>workerman<br \/>workerman\u662f\u4e00\u6b3e\u7eafPHP\u7f16\u5199\u7684\u975e\u963b\u585e\u670d\u52a1\u5668\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684API\uff0c\u4f7f\u5f97PHP\u5f00\u53d1\u8005\u53ef\u4ee5\u5feb\u901f\u5730\u6784\u5efa\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u5e94\u7528\u3002\u4e0eswoole\u76f8\u6bd4\uff0cworkerman\u66f4\u52a0\u8f7b\u91cf\u7ea7\uff0c\u6ca1\u6709\u4f9d\u8d56\u5176\u4ed6\u7684\u6269\u5c55\u3002<\/p>\n<p>\u76f8\u5bf9\u4e8eswoole\u800c\u8a00\uff0cworkerman\u7684\u5b66\u4e60\u66f2\u7ebf\u8f83\u4e3a\u5e73\u7f13\u3002\u8981\u5b66\u4e60workerman\uff0c\u53ea\u9700\u8981\u5177\u5907PHP\u57fa\u7840\u77e5\u8bc6\u5373\u53ef\u3002workerman\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u4e8e\u4f20\u7edfPHP\u5f00\u53d1\u7684\u7f16\u7a0b\u65b9\u5f0f\uff0c\u5f00\u53d1\u8005\u719f\u6089PHP\u7684\u8bed\u6cd5\u548c\u7f16\u7a0b\u601d\u60f3\u540e\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u4e0a\u624bworkerman\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684workerman\u670d\u52a1\u5668\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;?php \/\/ \u5f15\u5165workerman\u7684Autoloader\nrequire_once __DIR__ . '\/vendor\/autoload.php';\n\n\/\/ \u521b\u5efa\u4e00\u4e2aHTTP\u670d\u52a1\u5668\n$http = new WorkermanWorker('http:\/\/127.0.0.1:8080');\n\n\/\/ \u76d1\u542c\u8bf7\u6c42\u4e8b\u4ef6\n$http-&gt;onMessage = function($connection, $data) {\n    \/\/ \u5904\u7406\u8bf7\u6c42\n    $connection-&amp;gt;send(\"Hello, Workerman!\");\n};\n\n\/\/ \u542f\u52a8\u670d\u52a1\u5668\nWorkermanWorker::runAll();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5bf9\u6bd4\u4e0e\u603b\u7ed3<br \/>\u603b\u7684\u6765\u8bf4\uff0cswoole\u76f8\u5bf9\u4e8eworkerman\u6765\u8bf4\u5b66\u4e60\u66f2\u7ebf\u8f83\u4e3a\u9661\u5ced\u3002\u5b66\u4e60swoole\u9700\u8981\u8f83\u4e3a\u6df1\u5165\u7684\u5e95\u5c42\u7f51\u7edc\u77e5\u8bc6\u548cPHP\u7f16\u7a0b\u6280\u5de7\u3002\u800cworkerman\u5219\u66f4\u6ce8\u91cd\u4e8e\u63d0\u4f9b\u7b80\u5355\u6613\u7528\u7684API\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u4ee5\u5e38\u89c4\u7684PHP\u7f16\u7a0b\u65b9\u5f0f\u8fdb\u884c\u5f00\u53d1\u3002<\/p>\n<p>\u5bf9\u4e8ePHP\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u5efa\u8bae\u5148\u4ece\u5b66\u4e60workerman\u5f00\u59cb\u3002workerman\u63d0\u4f9b\u4e86\u826f\u597d\u7684\u5f00\u53d1\u6587\u6863\u548c\u4e30\u5bcc\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u53ef\u4ee5\u5f88\u5feb\u5165\u95e8\u3002\u5f85\u5bf9PHP\u7684\u7406\u89e3\u66f4\u52a0\u6df1\u5165\u540e\uff0c\u518d\u53ef\u4ee5\u8003\u8651\u5b66\u4e60swoole\uff0c\u4ee5\u6311\u6218\u66f4\u9ad8\u7ea7\u7684\u975e\u963b\u585e\u670d\u52a1\u5668\u5f00\u53d1\u3002<\/p>\n<p>\u603b\u7ed3\u8d77\u6765\uff0cswoole\u548cworkerman\u90fd\u662f\u975e\u5e38\u4f18\u79c0\u7684\u975e\u963b\u585e\u670d\u52a1\u5668\u5f00\u53d1\u6280\u672f\u3002\u5bf9\u4e8e\u6613\u5b66\u7a0b\u5ea6\u800c\u8a00\uff0cworkerman\u76f8\u5bf9\u66f4\u5bb9\u6613\u4e0a\u624b\uff0c\u800cswoole\u5219\u5177\u5907\u66f4\u9ad8\u7684\u6269\u5c55\u6027\u548c\u6027\u80fd\u3002\u8981\u9009\u62e9\u54ea\u4e00\u4e2a\u6280\u672f\u9700\u6839\u636e\u4e2a\u4eba\u7684\u5f00\u53d1\u7ecf\u9a8c\u548c\u9700\u6c42\u6765\u51b3\u5b9a\u3002<\/p>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<\/p>\n<ul>\n<li>swoole\u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/www.swoole.com\/<\/li>\n<li>workerman\u5b98\u65b9\u6587\u6863\uff1ahttp:\/\/www.workerman.net\/<\/li>\n<\/ul>\n<p>\u4ee3\u7801\u793a\u4f8b\u6765\u6e90\uff1a<\/p>\n<ul>\n<li>swoole\u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/www.swoole.com\/<\/li>\n<li>workerman\u5b98\u65b9\u6587\u6863\uff1ahttp:\/\/www.workerman.net\/<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u548c\u6280\u672f\u9009\u62e9\u6307\u5357\uff1a\u54ea\u4e2a\u66f4\u6613\u5b66\u4e60\uff1f\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>\u548c\u6280\u672f\u9009\u62e9\u6307\u5357\uff1a\u54ea\u4e2a\u66f4\u6613\u5b66\u4e60\uff1f \u5f15\u8a00\u5728\u5f53\u524d\u7684Web\u5f00\u53d1\u73af\u5883\u4e2d\uff0c\u975e\u963b\u585e\u7684\u670d\u52a1\u5668\u5f00\u53d1\u6280\u672f\u8d8a\u6765\u8d8a\u53d7\u5230\u5f00\u53d1\u8005\u7684\u5173\u6ce8\u3002\u5728\u8fd9\u5176\u4e2d\uff0cswoole\u548cworkerman\u65e0\u7591\u662f\u4e24\u4e2a\u5907\u53d7\u77a9\u76ee\u7684\u6280\u672f\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u6280\u672f\u53ef\u80fd\u4f1a\u662f\u4e00\u4e2a\u6311\u6218\u3002\u672c\u6587\u5c06\u4ece\u5b66\u4e60\u66f2\u7ebf\u7684\u89d2\u5ea6\uff0c\u6bd4\u8f83swoole\u548cworkerman\u7684\u6613\u5b66\u7a0b\u5ea6\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\uff0c\u4ee5\u5e2e\u52a9\u8bfb\u8005\u505a\u51fa\u9009\u62e9\u3002 swooleswoole\u662f\u4e00\u6b3e\u57fa\u4e8eC++\u8bed\u8a00\u7f16\u5199\u7684PHP\u6269\u5c55\uff0c\u63d0\u4f9b\u4e86\u4e00\u5957\u5b8c\u5584\u7684API\uff0c\u4f7f\u5f97PHP\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u4e0d\u5207\u6362\u8bed\u8a00\u7684\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7swoole\u9ad8\u6548\u5730\u5f00\u53d1\u975e\u963b\u585e\u7684\u670d\u52a1\u5668\u7a0b\u5e8f\u3002\u901a\u8fc7\u4f7f\u7528swoole\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5145\u5206\u5229\u7528PHP\u7684\u7075\u6d3b\u6027\u548c\u6613\u7528\u6027\uff0c\u540c\u65f6\u4eab\u53d7\u5230\u975e\u963b\u585eIO\u7684\u9ad8\u6027\u80fd\u3002 swoole\u7684\u5b66\u4e60\u66f2\u7ebf\u76f8\u5bf9\u6765\u8bf4\u8f83\u9661\u5ced\u3002\u8981\u5b66\u4e60swoole\uff0c\u9996\u5148\u9700\u8981\u638c\u63e1PHP\u57fa\u7840\u77e5\u8bc6\uff0c\u5305\u62ec\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u3001\u7f51\u7edc\u7f16\u7a0b\u7b49\u3002\u5176\u6b21\uff0c\u9700\u8981\u4e86\u89e3\u4e00\u4e9b\u5e95\u5c42\u7684\u7f51\u7edc\u901a\u4fe1\u539f\u7406\uff0c\u5982TCP\/IP\u534f\u8bae\u6808\u3001\u591a\u7ebf\u7a0b\u3001\u5f02\u6b65IO\u7b49\u3002\u6700\u540e\uff0c\u9700\u8981\u719f\u6089swoole\u63d0\u4f9b\u7684\u5404\u79cdAPI\uff0c\u5982Server\u3001Coroutine\u7b49\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684swoole\u670d\u52a1\u5668\u793a\u4f8b\u4ee3\u7801\uff1a &lt;?php \/\/ \u521b\u5efa\u4e00\u4e2aHTTP\u670d\u52a1\u5668 $http = new swoole_http_server(&#8220;127.0.0.1&#8221;, 9501); \/\/ \u76d1\u542c\u8bf7\u6c42\u4e8b\u4ef6 $http-&gt;on(&#8216;request&#8217;, function($request, $response) { \/\/ \u5904\u7406\u8bf7\u6c42 $response-&amp;gt;header(&#8220;Content-Type&#8221;, &#8220;text\/plain&#8221;); $response-&amp;gt;end(&#8220;Hello, Swoole!&#8221;); }); \/\/ \u542f\u52a8\u670d\u52a1\u5668 $http-&amp;gt;start(); \u767b\u5f55\u540e\u590d\u5236 workermanworkerman\u662f\u4e00\u6b3e\u7eafPHP\u7f16\u5199\u7684\u975e\u963b\u585e\u670d\u52a1\u5668\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684API\uff0c\u4f7f\u5f97PHP\u5f00\u53d1\u8005\u53ef\u4ee5\u5feb\u901f\u5730\u6784\u5efa\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u5e94\u7528\u3002\u4e0eswoole\u76f8\u6bd4\uff0cworkerman\u66f4\u52a0\u8f7b\u91cf\u7ea7\uff0c\u6ca1\u6709\u4f9d\u8d56\u5176\u4ed6\u7684\u6269\u5c55\u3002 \u76f8\u5bf9\u4e8eswoole\u800c\u8a00\uff0cworkerman\u7684\u5b66\u4e60\u66f2\u7ebf\u8f83\u4e3a\u5e73\u7f13\u3002\u8981\u5b66\u4e60workerman\uff0c\u53ea\u9700\u8981\u5177\u5907PHP\u57fa\u7840\u77e5\u8bc6\u5373\u53ef\u3002workerman\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u4e8e\u4f20\u7edfPHP\u5f00\u53d1\u7684\u7f16\u7a0b\u65b9\u5f0f\uff0c\u5f00\u53d1\u8005\u719f\u6089PHP\u7684\u8bed\u6cd5\u548c\u7f16\u7a0b\u601d\u60f3\u540e\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u4e0a\u624bworkerman\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684workerman\u670d\u52a1\u5668\u793a\u4f8b\u4ee3\u7801\uff1a &lt;?php \/\/ \u5f15\u5165workerman\u7684Autoloader require_once __DIR__ . &#8216;\/vendor\/autoload.php&#8217;; \/\/ \u521b\u5efa\u4e00\u4e2aHTTP\u670d\u52a1\u5668 $http = new WorkermanWorker(&#8216;http:\/\/127.0.0.1:8080&#8217;); \/\/ \u76d1\u542c\u8bf7\u6c42\u4e8b\u4ef6 $http-&gt;onMessage = function($connection, $data) { \/\/ \u5904\u7406\u8bf7\u6c42 [&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-24623","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24623","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=24623"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24623\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}