{"id":24620,"date":"2024-11-21T12:23:55","date_gmt":"2024-11-21T04:23:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24620\/"},"modified":"2024-11-21T12:23:55","modified_gmt":"2024-11-21T04:23:55","slug":"swoole%e5%92%8cworkerman%ef%bc%9a%e5%93%aa%e4%b8%aa%e6%9b%b4%e9%80%82%e5%90%88%e5%bf%ab%e9%80%9f%e5%bc%80%e5%8f%91%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24620\/","title":{"rendered":"swoole\u548cworkerman\uff1a\u54ea\u4e2a\u66f4\u9002\u5408\u5feb\u901f\u5f00\u53d1\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169423093864086.jpg\" class=\"aligncenter\" title=\"swoole\u548cworkerman\uff1a\u54ea\u4e2a\u66f4\u9002\u5408\u5feb\u901f\u5f00\u53d1\uff1f\u63d2\u56fe\" alt=\"swoole\u548cworkerman\uff1a\u54ea\u4e2a\u66f4\u9002\u5408\u5feb\u901f\u5f00\u53d1\uff1f\u63d2\u56fe\" \/><\/p>\n<p>\u548c\uff1a\u54ea\u4e2a\u66f4\u9002\u5408\u5feb\u901f\u5f00\u53d1\uff1f<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u968f\u7740WEB\u6280\u672f\u7684\u4e0d\u65ad\u53d1\u5c55\uff0cPHP\u4e5f\u4ece\u4e00\u4e2a\u7b80\u5355\u7684\u811a\u672c\u8bed\u8a00\u9010\u6e10\u53d1\u5c55\u6210\u4e3a\u9002\u7528\u4e8e\u9ad8\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7684\u8bed\u8a00\u4e4b\u4e00\u3002\u4f20\u7edf\u7684PHP\u5f00\u53d1\u6a21\u5f0f\u5728\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\u65f6\u663e\u5f97\u529b\u4e0d\u4ece\u5fc3\uff0c\u56e0\u6b64\u63a8\u51fa\u4e86\u4e00\u7cfb\u5217\u7684\u89e3\u51b3\u65b9\u6848\u3002\u800c\u8fd9\u5176\u4e2d\uff0cswoole\u548cworkerman\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8ePHP\u7684\u9ad8\u5e76\u53d1\u5f00\u53d1\u4e2d\u3002\u90a3\u4e48\uff0cswoole\u548cworkerman\u54ea\u4e2a\u66f4\u9002\u5408\u5feb\u901f\u5f00\u53d1\u5462\uff1f\u672c\u6587\u5c06\u4ece\u6027\u80fd\u3001\u4fbf\u6377\u6027\u7b49\u65b9\u9762\u8fdb\u884c\u6bd4\u8f83\uff0c\u5e76\u9644\u4e0a\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u6027\u80fd\u5bf9\u6bd4<\/p>\n<ol>\n<li>swoole\uff1a<br \/>swoole\u662f\u4e00\u4e2a\u57fa\u4e8e\u5185\u6838\u7684\u5168\u5f02\u6b65\u3001\u9ad8\u6027\u80fd\u7684PHP\u6269\u5c55\uff0c\u5e95\u5c42\u57fa\u4e8eC++\u7f16\u5199\u3002\u5b83\u63d0\u4f9b\u4e86TCP\/UDP\/Unix Socket\u4e24\u79cd\u5e76\u53d1\u6a21\u578b\uff0c\u652f\u6301\u4e00\u952e\u534f\u7a0b\u5316\uff0c\u4f7f\u5f97PHP\u5177\u5907\u4e86\u534f\u7a0b\u652f\u6301\u7684\u80fd\u529b\uff0c\u6781\u5927\u5730\u63d0\u5347\u4e86\u5e76\u53d1\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684swoole TCP\u670d\u52a1\u5668\u7684\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n<pre>$server = new swoole_server('0.0.0.0', 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);\n\n$server-&amp;gt;on('connect', function ($server, $fd) {\n    echo \"Client {$fd} connected.\n\";\n});\n\n$server-&amp;gt;on('receive', function ($server, $fd, $fromId, $data) {\n    $server-&amp;gt;send($fd, 'Server: ' . $data);\n});\n\n$server-&amp;gt;on('close', function ($server, $fd) {\n    echo \"Client {$fd} closed.\n\";\n});\n\n$server-&amp;gt;start();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>workerman\uff1a<br \/>workerman\u662f\u4e00\u4e2a\u7eafPHP\u5f00\u53d1\u7684\u5f02\u6b65\u975e\u963b\u585e\u7684\u9ad8\u6027\u80fd\u5e94\u7528\u670d\u52a1\u5668\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86TCP\/UDP\u534f\u8bae\u652f\u6301\u3002\u5de5\u4f5c\u539f\u7406\u4e0a\uff0cworkerman\u57fa\u4e8e\u591a\u8fdb\u7a0b\u3001Event Loop\u6a21\u578b\uff0c\u4f7f\u5f97PHP\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684workerman TCP\u670d\u52a1\u5668\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n<pre>require_once 'workerman\/Autoloader.php';\n\nuse WorkermanWorker;\n\n$worker = new Worker('tcp:\/\/0.0.0.0:9501');\n\n$worker-&amp;gt;onConnect = function ($connection) {\n    echo 'Client ' . $connection-&amp;gt;id . ' connected.' . PHP_EOL;\n};\n\n$worker-&amp;gt;onMessage = function ($connection, $data) {\n    $connection-&amp;gt;send('Server: ' . $data);\n};\n\n$worker-&amp;gt;onClose = function ($connection) {\n    echo 'Client ' . $connection-&amp;gt;id . ' closed.' . PHP_EOL;\n};\n\nWorker::runAll();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e8c\u3001\u4fbf\u6377\u6027\u5bf9\u6bd4<\/p>\n<ol>\n<li>swoole\uff1a<br \/>swoole\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7ec4\u4ef6\uff0c\u4f7f\u5f97\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u66f4\u52a0\u4fbf\u6377\u5730\u8fdb\u884c\u7f16\u5199\u548c\u7ba1\u7406\u3002\u901a\u8fc7swoole\u63d0\u4f9b\u7684\u534f\u7a0b\u652f\u6301\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5f02\u6b65\u7f16\u7a0b\u3002\u800c\u4e14swoole\u8fd8\u63d0\u4f9b\u4e86HTTP\/HTTPS\u670d\u52a1\u5668\u3001Websocket\u670d\u52a1\u5668\u7b49\u529f\u80fd\uff0c\u65b9\u4fbf\u5f00\u53d1\u8005\u5feb\u901f\u6784\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u5e94\u7528\u3002<\/li>\n<li>workerman\uff1a<br \/>\u4e0eswoole\u7c7b\u4f3c\uff0cworkerman\u4e5f\u63d0\u4f9b\u4e86\u5f88\u591a\u7ec4\u4ef6\uff0c\u65b9\u4fbf\u5f00\u53d1\u4eba\u5458\u5feb\u901f\u642d\u5efa\u670d\u52a1\u3002\u540c\u65f6\uff0cworkerman\u8fd8\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u76d1\u63a7\u548c\u7ba1\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u670d\u52a1\u5668\u8fdb\u884c\u7ba1\u7406\u548c\u76d1\u63a7\u3002<\/li>\n<\/ol>\n<p>\u4e09\u3001\u9009\u578b\u5efa\u8bae<br \/>swoole\u548cworkerman\u90fd\u662f\u975e\u5e38\u6210\u719f\u7684PHP\u5e76\u53d1\u5f00\u53d1\u6846\u67b6\uff0c\u6240\u4ee5\u5728\u5e94\u5bf9\u9ad8\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7684\u5f00\u53d1\u4efb\u52a1\u4e0a\uff0c\u5b83\u4eec\u90fd\u662f\u5f88\u597d\u7684\u9009\u62e9\u3002\u9009\u62e9\u54ea\u4e2a\u6846\u67b6\u8fd8\u5e94\u6839\u636e\u5177\u4f53\u7684\u5f00\u53d1\u9700\u6c42\u548c\u56e2\u961f\u7684\u6280\u672f\u5b9e\u529b\u6765\u51b3\u5b9a\u3002<\/p>\n<p>\u5982\u679c\u5f00\u53d1\u7684\u4e3b\u8981\u529f\u80fd\u662fTCP\/UDP\u901a\u4fe1\uff0c\u5e76\u4e14\u5bf9\u6027\u80fd\u8981\u6c42\u6781\u9ad8\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9swoole\u3002\u7531\u4e8eswoole\u7684\u5e95\u5c42\u57fa\u4e8eC++\uff0c\u6027\u80fd\u76f8\u5bf9\u8f83\u9ad8\u3002\u5e76\u4e14swoole\u8fd8\u652f\u6301\u534f\u7a0b\uff0c\u5bf9\u4e8e\u5f02\u6b65\u7f16\u7a0b\u548c\u9ad8\u5e76\u53d1\u7684\u573a\u666f\u975e\u5e38\u9002\u7528\u3002<\/p>\n<p>\u5982\u679c\u5f00\u53d1\u7684\u662f\u66f4\u4e3a\u590d\u6742\u548c\u5b8c\u6574\u7684\u5e94\u7528\uff0c\u4f8b\u5982Web\u670d\u52a1\u3001API\u670d\u52a1\u5668\u7b49\uff0cworkerman\u66f4\u9002\u5408\u3002workerman\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u7ec4\u4ef6\u548c\u529f\u80fd\uff0c\u65b9\u4fbf\u5f00\u53d1\u8005\u6784\u5efaWeb\u670d\u52a1\u5668\u3001\u5b9e\u65f6\u901a\u8baf\u670d\u52a1\u5668\u3001\u5206\u5e03\u5f0f\u5373\u65f6\u6d88\u606f\u63a8\u9001\u7cfb\u7edf\u7b49\u5e94\u7528\u3002<\/p>\n<p>\u7ed3\u8bba\uff1a<br \/>\u7efc\u4e0a\u6240\u8ff0\uff0cswoole\u548cworkerman\u90fd\u662f\u975e\u5e38\u4f18\u79c0\u7684PHP\u5e76\u53d1\u5f00\u53d1\u6846\u67b6\uff0c\u9009\u62e9\u54ea\u4e2a\u66f4\u9002\u5408\u5feb\u901f\u5f00\u53d1\u8981\u6839\u636e\u5b9e\u9645\u9700\u6c42\u6765\u5b9a\uff0c\u5982\u679c\u5bf9\u6027\u80fd\u8981\u6c42\u8f83\u9ad8\uff0c\u53ef\u4ee5\u9009\u62e9swoole\uff1b\u5982\u679c\u9700\u8981\u66f4\u5168\u9762\u3001\u590d\u6742\u7684\u5e94\u7528\u6846\u67b6\uff0c\u53ef\u4ee5\u9009\u62e9workerman\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u548c\uff1a\u54ea\u4e2a\u66f4\u9002\u5408\u5feb\u901f\u5f00\u53d1\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\uff1a\u54ea\u4e2a\u66f4\u9002\u5408\u5feb\u901f\u5f00\u53d1\uff1f \u5f15\u8a00\uff1a\u968f\u7740WEB\u6280\u672f\u7684\u4e0d\u65ad\u53d1\u5c55\uff0cPHP\u4e5f\u4ece\u4e00\u4e2a\u7b80\u5355\u7684\u811a\u672c\u8bed\u8a00\u9010\u6e10\u53d1\u5c55\u6210\u4e3a\u9002\u7528\u4e8e\u9ad8\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7684\u8bed\u8a00\u4e4b\u4e00\u3002\u4f20\u7edf\u7684PHP\u5f00\u53d1\u6a21\u5f0f\u5728\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\u65f6\u663e\u5f97\u529b\u4e0d\u4ece\u5fc3\uff0c\u56e0\u6b64\u63a8\u51fa\u4e86\u4e00\u7cfb\u5217\u7684\u89e3\u51b3\u65b9\u6848\u3002\u800c\u8fd9\u5176\u4e2d\uff0cswoole\u548cworkerman\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8ePHP\u7684\u9ad8\u5e76\u53d1\u5f00\u53d1\u4e2d\u3002\u90a3\u4e48\uff0cswoole\u548cworkerman\u54ea\u4e2a\u66f4\u9002\u5408\u5feb\u901f\u5f00\u53d1\u5462\uff1f\u672c\u6587\u5c06\u4ece\u6027\u80fd\u3001\u4fbf\u6377\u6027\u7b49\u65b9\u9762\u8fdb\u884c\u6bd4\u8f83\uff0c\u5e76\u9644\u4e0a\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u6027\u80fd\u5bf9\u6bd4 swoole\uff1aswoole\u662f\u4e00\u4e2a\u57fa\u4e8e\u5185\u6838\u7684\u5168\u5f02\u6b65\u3001\u9ad8\u6027\u80fd\u7684PHP\u6269\u5c55\uff0c\u5e95\u5c42\u57fa\u4e8eC++\u7f16\u5199\u3002\u5b83\u63d0\u4f9b\u4e86TCP\/UDP\/Unix Socket\u4e24\u79cd\u5e76\u53d1\u6a21\u578b\uff0c\u652f\u6301\u4e00\u952e\u534f\u7a0b\u5316\uff0c\u4f7f\u5f97PHP\u5177\u5907\u4e86\u534f\u7a0b\u652f\u6301\u7684\u80fd\u529b\uff0c\u6781\u5927\u5730\u63d0\u5347\u4e86\u5e76\u53d1\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684swoole TCP\u670d\u52a1\u5668\u7684\u793a\u4f8b\uff1a $server = new swoole_server(&#8216;0.0.0.0&#8217;, 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP); $server-&amp;gt;on(&#8216;connect&#8217;, function ($server, $fd) { echo &#8220;Client {$fd} connected. &#8220;; }); $server-&amp;gt;on(&#8216;receive&#8217;, function ($server, $fd, $fromId, $data) { $server-&amp;gt;send($fd, &#8216;Server: &#8216; . $data); }); $server-&amp;gt;on(&#8216;close&#8217;, function ($server, $fd) { echo &#8220;Client {$fd} closed. &#8220;; }); $server-&amp;gt;start(); \u767b\u5f55\u540e\u590d\u5236 workerman\uff1aworkerman\u662f\u4e00\u4e2a\u7eafPHP\u5f00\u53d1\u7684\u5f02\u6b65\u975e\u963b\u585e\u7684\u9ad8\u6027\u80fd\u5e94\u7528\u670d\u52a1\u5668\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86TCP\/UDP\u534f\u8bae\u652f\u6301\u3002\u5de5\u4f5c\u539f\u7406\u4e0a\uff0cworkerman\u57fa\u4e8e\u591a\u8fdb\u7a0b\u3001Event Loop\u6a21\u578b\uff0c\u4f7f\u5f97PHP\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684workerman TCP\u670d\u52a1\u5668\u793a\u4f8b\uff1a require_once &#8216;workerman\/Autoloader.php&#8217;; [&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-24620","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24620","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=24620"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24620\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}