{"id":1121,"date":"2025-01-20T23:20:18","date_gmt":"2025-01-20T15:20:18","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1121"},"modified":"2025-01-20T23:59:12","modified_gmt":"2025-01-20T15:59:12","slug":"workerman-%e7%8e%af%e5%a2%83%e6%90%ad%e5%bb%ba%e4%b8%8e%e5%ae%89%e8%a3%85%e6%95%99%e7%a8%8b%ef%bc%9a%e4%bb%8e%e9%9b%b6%e5%bc%80%e5%a7%8b%e9%85%8d%e7%bd%ae%e4%b8%8e%e9%83%a8%e7%bd%b2","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/20\/workerman-%e7%8e%af%e5%a2%83%e6%90%ad%e5%bb%ba%e4%b8%8e%e5%ae%89%e8%a3%85%e6%95%99%e7%a8%8b%ef%bc%9a%e4%bb%8e%e9%9b%b6%e5%bc%80%e5%a7%8b%e9%85%8d%e7%bd%ae%e4%b8%8e%e9%83%a8%e7%bd%b2\/","title":{"rendered":"Workerman \u73af\u5883\u642d\u5efa\u4e0e\u5b89\u88c5\u6559\u7a0b\uff1a\u4ece\u96f6\u5f00\u59cb\u914d\u7f6e\u4e0e\u90e8\u7f72"},"content":{"rendered":"\n<p>Workerman \u662f\u4e00\u4e2a\u975e\u5e38\u9ad8\u6548\u7684 <a href=\"https:\/\/www.php.net\">PHP \u7f51\u7edc\u6846\u67b6<\/a>\uff0c\u9002\u5408\u6784\u5efa\u9ad8\u5e76\u53d1\u7684\u5b9e\u65f6\u5e94\u7528\u3002\u5728\u5f00\u59cb\u4f7f\u7528 Workerman \u65f6\uff0c\u9996\u5148\u9700\u8981\u5b8c\u6210\u73af\u5883\u642d\u5efa\u4e0e\u5b89\u88c5\uff0c\u5e76\u914d\u7f6e\u5de5\u4f5c\u76ee\u5f55\u3001\u65e5\u5fd7\u7b49\u3002\u901a\u8fc7 Composer \u5b89\u88c5 Workerman\uff0c\u786e\u4fdd\u4f60\u7684 PHP \u73af\u5883\u4e0e\u6240\u9700\u6269\u5c55\u7684\u5b89\u88c5\uff0c\u63a5\u7740\u4f60\u53ef\u4ee5\u901a\u8fc7 PHP \u4ee3\u7801\u6765\u914d\u7f6e\u670d\u52a1\u5e76\u542f\u52a8\u3002\u5b8c\u6210\u8fd9\u4e9b\u6b65\u9aa4\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u5f00\u59cb\u6784\u5efa\u81ea\u5df1\u7684\u9ad8\u6027\u80fd\u7f51\u7edc\u670d\u52a1\u4e86\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1. \u5b89\u88c5 Workerman<\/strong><\/h4>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>(1) \u4f7f\u7528 Composer \u5b89\u88c5 Workerman<\/strong><\/h5>\n\n\n\n<p>Composer \u662f PHP \u7684\u4f9d\u8d56\u7ba1\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b89\u88c5\u548c\u7ba1\u7406\u9879\u76ee\u7684\u4f9d\u8d56\u5305\u3002\u8981\u5b89\u88c5 Workerman\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86 Composer\u3002\u7136\u540e\u53ef\u4ee5\u901a\u8fc7 Composer \u5b89\u88c5 Workerman\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b89\u88c5 Composer\uff08\u5982\u679c\u5c1a\u672a\u5b89\u88c5\uff09<\/strong><br>\u5982\u679c\u5c1a\u672a\u5b89\u88c5 Composer\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   curl -sS https:\/\/getcomposer.org\/installer | php<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u5c06 <code>composer.phar<\/code> \u6587\u4ef6\u79fb\u52a8\u5230\u7cfb\u7edf\u7684\u73af\u5883\u8def\u5f84\u4e2d\uff08\u5982 <code>\/usr\/local\/bin<\/code>\uff09\u3002<\/p>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u901a\u8fc7 Composer \u5b89\u88c5 Workerman<\/strong> \u8fdb\u5165\u4f60\u7684\u9879\u76ee\u76ee\u5f55\uff0c\u4f7f\u7528 Composer \u5b89\u88c5 Workerman\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   composer require workerman\/workerman<\/code><\/pre>\n\n\n\n<p>\u6216\u8005\u5982\u679c\u4f60\u5e0c\u671b\u5168\u5c40\u5b89\u88c5 Workerman\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   composer global require workerman\/workerman<\/code><\/pre>\n\n\n\n<p>\u8fd9\u5c06\u4f1a\u628a Workerman \u5b89\u88c5\u5230\u9879\u76ee\u76ee\u5f55\u7684 <code>vendor<\/code> \u76ee\u5f55\u4e0b\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>(2) \u914d\u7f6e PHP \u73af\u5883<\/strong><\/h5>\n\n\n\n<p>Workerman \u652f\u6301 PHP 7+ \u7248\u672c\uff0c\u5efa\u8bae\u4f7f\u7528 PHP 7.4 \u6216\u66f4\u9ad8\u7248\u672c\u3002\u786e\u4fdd\u4f60\u7684 PHP \u73af\u5883\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5b89\u88c5 PHP 7.4+<\/strong><br>\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f Linux\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5 PHP\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  sudo apt-get update\n  sudo apt-get install php7.4 php7.4-cli php7.4-mbstring php7.4-xml php7.4-curl php7.4-json<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u786e\u8ba4 PHP \u7248\u672c<\/strong><br>\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5\u4f60\u7684 PHP \u7248\u672c\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  php -v<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5b89\u88c5\u5fc5\u8981\u7684 PHP \u6269\u5c55<\/strong><br>Workerman \u4f9d\u8d56\u4e8e\u4e00\u4e9b\u5e38\u7528\u7684 PHP \u6269\u5c55\uff0c\u5982 <code>pcntl<\/code>\u3001<code>mbstring<\/code> \u7b49\u3002\u786e\u4fdd\u4f60\u7684 PHP \u5b89\u88c5\u4e2d\u5305\u542b\u4ee5\u4e0b\u6269\u5c55\uff1a<\/li>\n\n\n\n<li><code>pcntl<\/code>\uff1a\u7528\u4e8e\u652f\u6301\u591a\u8fdb\u7a0b\u3002<\/li>\n\n\n\n<li><code>mbstring<\/code>\uff1a\u7528\u4e8e\u591a\u5b57\u8282\u5b57\u7b26\u652f\u6301\uff08\u5982\u4e2d\u6587\uff09\u3002<\/li>\n\n\n\n<li><code>json<\/code>\uff1a\u7528\u4e8e\u5904\u7406 JSON \u6570\u636e\u3002<\/li>\n\n\n\n<li><code>curl<\/code>\uff1a\u7528\u4e8e HTTP \u8bf7\u6c42\u3002 \u5728 Ubuntu \u7cfb\u7edf\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  sudo apt-get install php7.4-pcntl php7.4-mbstring php7.4-curl php7.4-json<\/code><\/pre>\n\n\n\n<p>\u5728 Windows \u73af\u5883\u4e0b\uff0c<code>pcntl<\/code> \u6269\u5c55\u9ed8\u8ba4\u4e0d\u652f\u6301\u3002\u5982\u679c\u662f\u5728 Windows \u4e0a\u5f00\u53d1\uff0c\u5efa\u8bae\u5728 WSL\uff08Windows Subsystem for Linux\uff09\u73af\u5883\u4e2d\u4f7f\u7528\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2. \u57fa\u672c\u914d\u7f6e<\/strong><\/h4>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>(1) Workerman \u7684\u914d\u7f6e\u6587\u4ef6\u7ed3\u6784<\/strong><\/h5>\n\n\n\n<p>Workerman \u7684\u914d\u7f6e\u5927\u591a\u662f\u901a\u8fc7 PHP \u4ee3\u7801\u8fdb\u884c\u8bbe\u7f6e\u7684\uff0c\u901a\u5e38\u6ca1\u6709\u5355\u72ec\u7684\u914d\u7f6e\u6587\u4ef6\u3002\u914d\u7f6e\u9879\u5728 Worker \u7c7b\u53ca\u5176\u5b50\u7c7b\u4e2d\u8fdb\u884c\u8bbe\u7f6e\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u914d\u7f6e WebSocket \u670d\u52a1\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8fdb\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use Workerman\\Worker;\n\n$worker = new Worker('websocket:\/\/0.0.0.0:8080');\n\n\/\/ \u8bbe\u7f6e\u8fdb\u7a0b\u6570\u91cf\n$worker-&gt;count = 4;\n\n\/\/ \u8bbe\u7f6e\u542f\u52a8\u51fd\u6570\n$worker-&gt;onWorkerStart = function($worker) {\n    echo \"Worker started\\n\";\n};\n\nWorker::runAll();<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>websocket:\/\/0.0.0.0:8080<\/code> \u662f\u670d\u52a1\u7684\u76d1\u542c\u5730\u5740\uff0c<code>count<\/code> \u662f\u8bbe\u7f6e\u5de5\u4f5c\u8fdb\u7a0b\u6570\uff0c<code>onWorkerStart<\/code> \u662f\u5de5\u4f5c\u8fdb\u7a0b\u542f\u52a8\u65f6\u8c03\u7528\u7684\u56de\u8c03\u51fd\u6570\u3002<\/p>\n\n\n\n<p><strong>\u5e38\u89c1\u914d\u7f6e\u9879\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>count<\/strong>\uff1a\u8bbe\u7f6e\u8fdb\u7a0b\u6570\uff0c\u51b3\u5b9a\u4e86 Workerman \u542f\u52a8\u65f6\u521b\u5efa\u7684\u5b50\u8fdb\u7a0b\u6570\u3002\u901a\u5e38\uff0c\u8fdb\u7a0b\u6570\u5e94\u6839\u636e\u670d\u52a1\u5668\u7684 CPU \u6838\u5fc3\u6570\u8fdb\u884c\u8bbe\u7f6e\u3002<\/li>\n\n\n\n<li><strong>onWorkerStart<\/strong>\uff1a\u5de5\u4f5c\u8fdb\u7a0b\u542f\u52a8\u65f6\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570\u3002<\/li>\n\n\n\n<li><strong>onMessage<\/strong>\uff1a\u8bbe\u7f6e\u63a5\u6536\u5230\u5ba2\u6237\u7aef\u6d88\u606f\u65f6\u7684\u56de\u8c03\u51fd\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>(2) \u914d\u7f6e\u5de5\u4f5c\u76ee\u5f55\u548c\u65e5\u5fd7<\/strong><\/h5>\n\n\n\n<p>\u867d\u7136 Workerman \u9ed8\u8ba4\u4f1a\u5728\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e0b\u8fd0\u884c\uff0c\u4f46\u4e3a\u4e86\u66f4\u597d\u7684\u7ba1\u7406\uff0c\u4f60\u53ef\u4ee5\u663e\u5f0f\u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\u548c\u65e5\u5fd7\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55<\/strong>\uff1a<br>\u4f7f\u7528 <code>WORKER_RUNNING_DIR<\/code> \u73af\u5883\u53d8\u91cf\u6216\u5728\u4ee3\u7801\u4e2d\u8bbe\u7f6e <code>Worker::set<\/code> \u65b9\u6cd5\u6765\u6307\u5b9a\u5de5\u4f5c\u76ee\u5f55\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  Worker::set(&#91;\n      'worker_name' =&gt; 'MyWorker',\n      'log_file' =&gt; '\/path\/to\/log\/file.log',\n      'pid_file' =&gt; '\/path\/to\/pid\/file.pid',\n  ]);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bbe\u7f6e\u65e5\u5fd7\u6587\u4ef6<\/strong>\uff1a<br>Workerman \u652f\u6301\u65e5\u5fd7\u8f93\u51fa\uff0c\u4f60\u53ef\u4ee5\u6307\u5b9a\u65e5\u5fd7\u6587\u4ef6\u7684\u4f4d\u7f6e\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cWorkerman \u4f1a\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa\u3002\u5982\u679c\u4f60\u5e0c\u671b\u5c06\u65e5\u5fd7\u5199\u5165\u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>log_file<\/code> \u914d\u7f6e\u9879\u8fdb\u884c\u8bbe\u7f6e\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  Worker::set(&#91;\n      'log_file' =&gt; '\/path\/to\/workerman.log',\n  ]);<\/code><\/pre>\n\n\n\n<p>\u6b64\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e <code>log_level<\/code> \u6765\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  Worker::set(&#91;\n      'log_level' =&gt; 3, \/\/ \u8f93\u51fa\u6240\u6709\u65e5\u5fd7\n  ]);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bbe\u7f6e PID \u6587\u4ef6<\/strong>\uff1a<br>\u8bbe\u7f6e\u8fdb\u7a0b\u7684 PID \u6587\u4ef6\uff0c\u4fbf\u4e8e\u7ba1\u7406\u8fdb\u7a0b\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  Worker::set(&#91;\n      'pid_file' =&gt; '\/path\/to\/worker.pid',\n  ]);<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>(3) \u542f\u52a8\u548c\u505c\u6b62 Workerman \u670d\u52a1<\/strong><\/h5>\n\n\n\n<p>Workerman \u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u542f\u52a8\u548c\u505c\u6b62\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u542f\u52a8\u670d\u52a1<\/strong>\uff1a\u901a\u8fc7\u547d\u4ee4\u884c\u542f\u52a8\u670d\u52a1\uff0c\u8fdb\u5165\u9879\u76ee\u6839\u76ee\u5f55\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  php start.php start<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u7684 <code>start.php<\/code> \u662f\u4f60\u521b\u5efa\u7684\u542f\u52a8\u6587\u4ef6\uff08\u901a\u5e38\u4e3a <code>start.php<\/code> \u6216\u5176\u4ed6\u81ea\u5b9a\u4e49\u540d\u79f0\uff09\u3002\u5728\u6587\u4ef6\u4e2d\uff0c\u901a\u8fc7 <code>Worker::runAll()<\/code> \u542f\u52a8\u6240\u6709\u5de5\u4f5c\u8fdb\u7a0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  require_once '.\/vendor\/autoload.php';  \/\/ \u5f15\u5165 Composer \u81ea\u52a8\u52a0\u8f7d\u6587\u4ef6\n  use Workerman\\Worker;\n\n  \/\/ \u521b\u5efa\u4e00\u4e2a WebSocket \u670d\u52a1\n  $worker = new Worker(\"websocket:\/\/0.0.0.0:8080\");\n  $worker-&gt;count = 4;\n\n  \/\/ \u542f\u52a8\u670d\u52a1\n  Worker::runAll();<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u505c\u6b62\u670d\u52a1<\/strong>\uff1a\u505c\u6b62 Workerman \u670d\u52a1\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  php start.php stop<\/code><\/pre>\n\n\n\n<p>Workerman \u4f1a\u81ea\u52a8\u6839\u636e <code>start.php<\/code> \u6587\u4ef6\u4e2d\u7684 PID \u6587\u4ef6\u6765\u505c\u6b62\u76f8\u5173\u8fdb\u7a0b\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u67e5\u770b\u72b6\u6001<\/strong>\uff1a\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u67e5\u770b Workerman \u7684\u72b6\u6001\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  php start.php status<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Workerman \u662f\u4e00\u4e2a\u975e\u5e38\u9ad8\u6548\u7684 PHP \u7f51\u7edc\u6846\u67b6\uff0c\u9002\u5408\u6784\u5efa\u9ad8\u5e76\u53d1\u7684\u5b9e\u65f6\u5e94\u7528\u3002\u5728\u5f00\u59cb\u4f7f\u7528 Workerma [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-1121","post","type-post","status-publish","format-standard","hentry","category-workerman-php-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1121"}],"version-history":[{"count":2,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1121\/revisions"}],"predecessor-version":[{"id":1140,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1121\/revisions\/1140"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}