{"id":24607,"date":"2024-11-21T14:49:51","date_gmt":"2024-11-21T06:49:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24607\/"},"modified":"2024-11-21T14:49:51","modified_gmt":"2024-11-21T06:49:51","slug":"%e5%a6%82%e4%bd%95%e9%80%89%e6%8b%a9%ef%bc%9aswoole%e4%b8%8eworkerman%e5%bc%80%e5%8f%91%e6%8a%80%e6%9c%af%e5%af%b9%e6%af%94","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24607\/","title":{"rendered":"\u5982\u4f55\u9009\u62e9\uff1aswoole\u4e0eworkerman\u5f00\u53d1\u6280\u672f\u5bf9\u6bd4"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169414284441251.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u9009\u62e9\uff1aswoole\u4e0eworkerman\u5f00\u53d1\u6280\u672f\u5bf9\u6bd4\u63d2\u56fe\" alt=\"\u5982\u4f55\u9009\u62e9\uff1aswoole\u4e0eworkerman\u5f00\u53d1\u6280\u672f\u5bf9\u6bd4\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u9009\u62e9\uff1aSwoole\u4e0eWorkerman\u5f00\u53d1\u6280\u672f\u5bf9\u6bd4<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u5728\u73b0\u4ee3\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u4e2d\uff0c\u9009\u62e9\u4e00\u79cd\u9002\u5408\u7684\u5f00\u53d1\u6280\u672f\u975e\u5e38\u91cd\u8981\u3002Swoole\u548cWorkerman\u90fd\u662fPHP\u7a0b\u5e8f\u5458\u5728\u5f00\u53d1\u9ad8\u6027\u80fd\u7f51\u7edc\u5e94\u7528\u65f6\u5e38\u7528\u7684\u5de5\u5177\u3002\u672c\u6587\u5c06\u5bf9\u8fd9\u4e24\u79cd\u6280\u672f\u8fdb\u884c\u5168\u9762\u7684\u5bf9\u6bd4\uff0c\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u9009\u62e9\u9002\u5408\u81ea\u5df1\u9879\u76ee\u7684\u5f00\u53d1\u6280\u672f\u3002<\/p>\n<p>\u4e00\u3001\u6982\u8ff0<br \/>Swoole\u662f\u4e00\u4e2a\u57fa\u4e8ePHP\u7684\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u5f15\u64ce\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f02\u6b65\u3001\u975e\u963b\u585e\u7684\u7f51\u7edc\u7f16\u7a0b\u63a5\u53e3\uff0c\u9002\u7528\u4e8e\u5f00\u53d1\u9ad8\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u5e94\u7528\u3002Workerman\u662f\u53e6\u4e00\u79cdPHP\u7f51\u7edc\u7f16\u7a0b\u6846\u67b6\uff0c\u901a\u8fc7\u4f7f\u7528\u591a\u8fdb\u7a0b\u548c\u4e8b\u4ef6\u8f6e\u8be2\u6280\u672f\u6784\u5efa\u9ad8\u6027\u80fd\u7f51\u7edc\u5e94\u7528\u3002\u5b83\u4eec\u90fd\u5177\u6709\u9ad8\u6027\u80fd\u7684\u7279\u70b9\uff0c\u4f46\u5404\u6709\u4e0d\u540c\u7684\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<p>\u4e8c\u3001\u6027\u80fd\u5bf9\u6bd4<\/p>\n<ol>\n<li>\u5e76\u53d1\u8fde\u63a5\u6570<br \/>\u5728\u5e76\u53d1\u8fde\u63a5\u6570\u65b9\u9762\uff0cSwoole\u76f8\u5bf9\u4e8eWorkerman\u5177\u6709\u66f4\u9ad8\u7684\u6027\u80fd\u3002Swoole\u57fa\u4e8e\u5f02\u6b65\u975e\u963b\u585e\u7684\u65b9\u5f0f\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8fde\u63a5\uff0c\u9002\u7528\u4e8e\u9ad8\u6027\u80fd\u7684\u901a\u4fe1\u9700\u6c42\u3002\u800cWorkerman\u91c7\u7528\u591a\u8fdb\u7a0b\u6a21\u578b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u8d1f\u8d23\u72ec\u7acb\u7684\u8fde\u63a5\uff0c\u540c\u65f6\u4e5f\u80fd\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8fde\u63a5\u3002<\/li>\n<li>\u8fdb\u7a0b\u7ba1\u7406<br \/>\u5728\u8fdb\u7a0b\u7ba1\u7406\u65b9\u9762\uff0cWorkerman\u66f4\u52a0\u7075\u6d3b\u3002Workerman\u901a\u8fc7\u591a\u8fdb\u7a0b\u6a21\u578b\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u542f\u52a8\u548c\u7ba1\u7406\u8fdb\u7a0b\uff0c\u65b9\u4fbf\u7075\u6d3b\u5730\u8c03\u6574\u8d44\u6e90\u7684\u4f7f\u7528\u3002\u800cSwoole\u5219\u662f\u91c7\u7528\u4e00\u4e2a\u6216\u591a\u4e2a\u8fdb\u7a0b\u5904\u7406\u6240\u6709\u7684\u8fde\u63a5\uff0c\u8fdb\u7a0b\u4e4b\u95f4\u8f83\u5c11\u901a\u4fe1\uff0c\u4f46\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8d44\u6e90\u7684\u6d6a\u8d39\u3002<\/li>\n<\/ol>\n<p>\u4e09\u3001\u7f16\u7a0b\u6a21\u578b\u548c\u4ee3\u7801\u793a\u4f8b<\/p>\n<ol>\n<li>Swoole\u7f16\u7a0b\u6a21\u578b<br \/>Swoole\u63d0\u4f9b\u4e86\u534f\u7a0b\u652f\u6301\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4f7f\u7528\u540c\u6b65\u7f16\u7a0b\u6a21\u578b\u7f16\u5199\u5f02\u6b65\u4ee3\u7801\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Swoole\u8fdb\u884c\u7f51\u7edc\u7f16\u7a0b\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n<pre>&lt;?php $server = new SwooleServer(\"0.0.0.0\", 9501);\n\n$server-&gt;on('connect', function ($server, $fd){\n    echo \"Client: Connect.\n\";\n});\n\n$server-&amp;gt;on('receive', function ($server, $fd, $from_id, $data) {\n    $server-&amp;gt;send($fd, \"Server: \" . $data);\n});\n\n$server-&amp;gt;on('close', function ($server, $fd) {\n    echo \"Client: Close.\n\";\n});\n\n$server-&amp;gt;start();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>Workerman\u7f16\u7a0b\u6a21\u578b<br \/>Workerman\u91c7\u7528\u4e8b\u4ef6\u9a71\u52a8\u7684\u7f16\u7a0b\u6a21\u578b\uff0c\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8fde\u63a5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Workerman\u8fdb\u884c\u7f51\u7edc\u7f16\u7a0b\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n<pre>&lt;?php require_once __DIR__ . '\/vendor\/autoload.php';\n\nuse WorkermanWorker;\n\n$worker = new Worker(\"tcp:\/\/0.0.0.0:2345\");\n\n$worker-&gt;count = 4;\n\n$worker-&amp;gt;onMessage = function($connection, $data)\n{\n    $connection-&amp;gt;send('Hello ' . $data);\n};\n\nWorker::runAll();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u56db\u3001\u751f\u6001\u7cfb\u7edf\u548c\u793e\u533a\u652f\u6301<br \/>Swoole\u548cWorkerman\u90fd\u6709\u5e9e\u5927\u7684\u5f00\u6e90\u793e\u533a\uff0c\u62e5\u6709\u4e30\u5bcc\u7684\u7b2c\u4e09\u65b9\u5e93\u548c\u6269\u5c55\u3002Swoole\u5728PHP\u7684\u9ad8\u6027\u80fd\u7f51\u7edc\u5e94\u7528\u5f00\u53d1\u65b9\u9762\u66f4\u52a0\u6210\u719f\uff0c\u5e76\u4e14\u5728\u751f\u6001\u7cfb\u7edf\u548c\u793e\u533a\u652f\u6301\u4e0a\u66f4\u4e3a\u6d3b\u8dc3\u3002Workerman\u5219\u5728\u4e00\u4e9b\u7279\u5b9a\u573a\u666f\uff08\u5982\u5b9e\u65f6\u804a\u5929\uff0c\u6e38\u620f\u670d\u52a1\u5668\u7b49\uff09\u4e2d\u66f4\u52a0\u53d7\u6b22\u8fce\uff0c\u793e\u533a\u4e5f\u6709\u5f88\u591a\u76f8\u5173\u7684\u6269\u5c55\u548c\u6848\u4f8b\u3002<\/p>\n<p>\u4e94\u3001\u9002\u7528\u573a\u666f\u9009\u62e9<br \/>\u9009\u62e9\u5408\u9002\u7684\u5f00\u53d1\u6280\u672f\u9700\u8981\u7efc\u5408\u8003\u8651\u9879\u76ee\u7684\u9700\u6c42\u3002\u5982\u679c\u9879\u76ee\u5bf9\u6027\u80fd\u6709\u975e\u5e38\u9ad8\u7684\u8981\u6c42\uff0c\u4e14\u5b9e\u73b0\u8f83\u4e3a\u590d\u6742\uff0cSwoole\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u5982\u679c\u9879\u76ee\u5bf9\u6027\u80fd\u8981\u6c42\u8f83\u9ad8\uff0c\u4f46\u8f83\u4e3a\u7b80\u5355\uff0cWorkerman\u4e5f\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002<\/p>\n<p>\u516d\u3001\u603b\u7ed3<br \/>Swoole\u548cWorkerman\u90fd\u662fPHP\u7a0b\u5e8f\u5458\u5f00\u53d1\u9ad8\u6027\u80fd\u7f51\u7edc\u5e94\u7528\u7684\u597d\u5de5\u5177\uff0c\u5b83\u4eec\u5728\u6027\u80fd\u3001\u7f16\u7a0b\u6a21\u578b\u3001\u751f\u6001\u7cfb\u7edf\u548c\u9002\u7528\u573a\u666f\u7b49\u65b9\u9762\u5b58\u5728\u4e00\u4e9b\u5dee\u5f02\u3002\u9009\u62e9\u9002\u5408\u81ea\u5df1\u9879\u76ee\u7684\u5f00\u53d1\u6280\u672f\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9879\u76ee\u9700\u6c42\u548c\u9879\u76ee\u89c4\u6a21\uff0c\u7efc\u5408\u8003\u8651\u8fd9\u4e9b\u56e0\u7d20\uff0c\u8fdb\u884c\u6743\u8861\u548c\u9009\u62e9\u3002<\/p>\n<p>\u53c2\u8003\u6587\u732e\uff1a<\/p>\n<ol>\n<li>Swoole\u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/www..com\/<\/li>\n<li>Workerman\u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/www..net\/<\/li>\n<\/ol>\n<p>\uff08\u5b57\u6570\uff1a749\uff09<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u9009\u62e9\uff1a\u4e0e\u5f00\u53d1\u6280\u672f\u5bf9\u6bd4\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>\u5982\u4f55\u9009\u62e9\uff1aSwoole\u4e0eWorkerman\u5f00\u53d1\u6280\u672f\u5bf9\u6bd4 \u5f15\u8a00\uff1a\u5728\u73b0\u4ee3\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u4e2d\uff0c\u9009\u62e9\u4e00\u79cd\u9002\u5408\u7684\u5f00\u53d1\u6280\u672f\u975e\u5e38\u91cd\u8981\u3002Swoole\u548cWorkerman\u90fd\u662fPHP\u7a0b\u5e8f\u5458\u5728\u5f00\u53d1\u9ad8\u6027\u80fd\u7f51\u7edc\u5e94\u7528\u65f6\u5e38\u7528\u7684\u5de5\u5177\u3002\u672c\u6587\u5c06\u5bf9\u8fd9\u4e24\u79cd\u6280\u672f\u8fdb\u884c\u5168\u9762\u7684\u5bf9\u6bd4\uff0c\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u9009\u62e9\u9002\u5408\u81ea\u5df1\u9879\u76ee\u7684\u5f00\u53d1\u6280\u672f\u3002 \u4e00\u3001\u6982\u8ff0Swoole\u662f\u4e00\u4e2a\u57fa\u4e8ePHP\u7684\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u5f15\u64ce\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f02\u6b65\u3001\u975e\u963b\u585e\u7684\u7f51\u7edc\u7f16\u7a0b\u63a5\u53e3\uff0c\u9002\u7528\u4e8e\u5f00\u53d1\u9ad8\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u5e94\u7528\u3002Workerman\u662f\u53e6\u4e00\u79cdPHP\u7f51\u7edc\u7f16\u7a0b\u6846\u67b6\uff0c\u901a\u8fc7\u4f7f\u7528\u591a\u8fdb\u7a0b\u548c\u4e8b\u4ef6\u8f6e\u8be2\u6280\u672f\u6784\u5efa\u9ad8\u6027\u80fd\u7f51\u7edc\u5e94\u7528\u3002\u5b83\u4eec\u90fd\u5177\u6709\u9ad8\u6027\u80fd\u7684\u7279\u70b9\uff0c\u4f46\u5404\u6709\u4e0d\u540c\u7684\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\u3002 \u4e8c\u3001\u6027\u80fd\u5bf9\u6bd4 \u5e76\u53d1\u8fde\u63a5\u6570\u5728\u5e76\u53d1\u8fde\u63a5\u6570\u65b9\u9762\uff0cSwoole\u76f8\u5bf9\u4e8eWorkerman\u5177\u6709\u66f4\u9ad8\u7684\u6027\u80fd\u3002Swoole\u57fa\u4e8e\u5f02\u6b65\u975e\u963b\u585e\u7684\u65b9\u5f0f\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8fde\u63a5\uff0c\u9002\u7528\u4e8e\u9ad8\u6027\u80fd\u7684\u901a\u4fe1\u9700\u6c42\u3002\u800cWorkerman\u91c7\u7528\u591a\u8fdb\u7a0b\u6a21\u578b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u8d1f\u8d23\u72ec\u7acb\u7684\u8fde\u63a5\uff0c\u540c\u65f6\u4e5f\u80fd\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8fde\u63a5\u3002 \u8fdb\u7a0b\u7ba1\u7406\u5728\u8fdb\u7a0b\u7ba1\u7406\u65b9\u9762\uff0cWorkerman\u66f4\u52a0\u7075\u6d3b\u3002Workerman\u901a\u8fc7\u591a\u8fdb\u7a0b\u6a21\u578b\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u542f\u52a8\u548c\u7ba1\u7406\u8fdb\u7a0b\uff0c\u65b9\u4fbf\u7075\u6d3b\u5730\u8c03\u6574\u8d44\u6e90\u7684\u4f7f\u7528\u3002\u800cSwoole\u5219\u662f\u91c7\u7528\u4e00\u4e2a\u6216\u591a\u4e2a\u8fdb\u7a0b\u5904\u7406\u6240\u6709\u7684\u8fde\u63a5\uff0c\u8fdb\u7a0b\u4e4b\u95f4\u8f83\u5c11\u901a\u4fe1\uff0c\u4f46\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8d44\u6e90\u7684\u6d6a\u8d39\u3002 \u4e09\u3001\u7f16\u7a0b\u6a21\u578b\u548c\u4ee3\u7801\u793a\u4f8b Swoole\u7f16\u7a0b\u6a21\u578bSwoole\u63d0\u4f9b\u4e86\u534f\u7a0b\u652f\u6301\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4f7f\u7528\u540c\u6b65\u7f16\u7a0b\u6a21\u578b\u7f16\u5199\u5f02\u6b65\u4ee3\u7801\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Swoole\u8fdb\u884c\u7f51\u7edc\u7f16\u7a0b\u7684\u4ee3\u7801\u793a\u4f8b\uff1a &lt;?php $server = new SwooleServer(&#8220;0.0.0.0&#8221;, 9501); $server-&gt;on(&#8216;connect&#8217;, function ($server, $fd){ echo &#8220;Client: Connect. &#8220;; }); $server-&amp;gt;on(&#8216;receive&#8217;, function ($server, $fd, $from_id, $data) { $server-&amp;gt;send($fd, &#8220;Server: &#8221; . $data); }); $server-&amp;gt;on(&#8216;close&#8217;, function ($server, $fd) { echo &#8220;Client: Close. &#8220;; }); $server-&amp;gt;start(); \u767b\u5f55\u540e\u590d\u5236 Workerman\u7f16\u7a0b\u6a21\u578bWorkerman\u91c7\u7528\u4e8b\u4ef6\u9a71\u52a8\u7684\u7f16\u7a0b\u6a21\u578b\uff0c\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8fde\u63a5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Workerman\u8fdb\u884c\u7f51\u7edc\u7f16\u7a0b\u7684\u4ee3\u7801\u793a\u4f8b\uff1a &lt;?php require_once __DIR__ . &#8216;\/vendor\/autoload.php&#8217;; use [&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-24607","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24607","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=24607"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24607\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}