{"id":25033,"date":"2024-11-21T12:22:45","date_gmt":"2024-11-21T04:22:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25033\/"},"modified":"2024-11-21T12:22:45","modified_gmt":"2024-11-21T04:22:45","slug":"%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0workerman%e6%96%87%e6%a1%a3%e7%9a%84%e5%9f%ba%e6%9c%ac%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25033\/","title":{"rendered":"\u5982\u4f55\u5b9e\u73b0Workerman\u6587\u6863\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/465\/014\/169941521359344.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5b9e\u73b0Workerman\u6587\u6863\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\u63d2\u56fe\" alt=\"\u5982\u4f55\u5b9e\u73b0Workerman\u6587\u6863\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u5b9e\u73b0 Workerman \u6587\u6863\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/p>\n<p>\u7b80\u4ecb\uff1a<br \/>Workerman \u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684PHP\u5f00\u53d1\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u8f7b\u677e\u6784\u5efa\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c06\u4ecb\u7ecd Workerman \u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\uff0c\u5305\u62ec\u5b89\u88c5\u548c\u914d\u7f6e\u3001\u521b\u5efa\u670d\u52a1\u548c\u76d1\u542c\u7aef\u53e3\u3001\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42\u7b49\u3002\u5e76\u7ed9\u51fa\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u5b89\u88c5\u548c\u914d\u7f6e Workerman<\/p>\n<ol>\n<li>\n<p>\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5 Workerman\uff1a<\/p>\n<pre>composer require workerman\/workerman<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/li>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6 server.php\uff0c\u5e76\u5bfc\u5165 WorkermanWorker\uff1a<\/p>\n<pre>require_once __DIR__ . '\/vendor\/autoload.php';\n\nuse WorkermanWorker;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/li>\n<li>\n<p>\u914d\u7f6e Workerman \u7684\u8fd0\u884c\u53c2\u6570\uff1a<\/p>\n<pre>$worker = new Worker('tcp:\/\/0.0.0.0:1234');\n$worker-&amp;gt;count = 4;\n$worker-&amp;gt;name = 'MyWorker';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff0ctcp:\/\/0.0.0.0:1234 \u8868\u793a\u76d1\u542c\u672c\u5730\u7684 1234 \u7aef\u53e3\uff0ccount \u8868\u793a\u542f\u52a8 4 \u4e2a worker \u8fdb\u7a0b\uff0cname \u8868\u793a\u7ed9\u5f53\u524d worker \u8bbe\u7f6e\u4e00\u4e2a\u540d\u79f0\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u4e8c\u3001\u521b\u5efa\u670d\u52a1\u548c\u76d1\u542c\u7aef\u53e3<\/p>\n<ol>\n<li>\n<p>\u5728 server.php \u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u670d\u52a1\u5e76\u76d1\u542c\u7aef\u53e3\uff1a<\/p>\n<pre>$worker-&amp;gt;onWorkerStart = function($worker) {\n echo \"Worker {$worker-&amp;gt;id} started\n\";\n};\n\n$worker-&amp;gt;onConnect = function($connection) {\n echo \"Connection established: {$connection-&amp;gt;id}\n\";\n};\n\n$worker-&amp;gt;onMessage = function($connection, $data) {\n echo \"Received data: {$data}\n\";\n $connection-&amp;gt;send(\"Hello, $data\");\n};\n\n$worker-&amp;gt;onClose = function($connection) {\n echo \"Connection closed: {$connection-&amp;gt;id}\n\";\n};\n\nWorker::runAll();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/li>\n<li>\n<p>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c server.php\uff1a<\/p>\n<pre>php server.php start<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6837\u5c31\u521b\u5efa\u4e86\u4e00\u4e2a\u76d1\u542c\u672c\u5730 1234 \u7aef\u53e3\u7684\u670d\u52a1\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u4e09\u3001\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42<\/p>\n<ol>\n<li>\n<p>\u5728\u53e6\u4e00\u4e2a\u7ec8\u7aef\u6216\u6d4f\u89c8\u5668\u4e2d\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u8fde\u63a5\u5230\u670d\u52a1\u7aef\uff1a<\/p>\n<pre>telnet localhost 1234<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/li>\n<li>\u8fde\u63a5\u6210\u529f\u540e\uff0c\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165\u4efb\u610f\u5b57\u7b26\u4e32\uff0c\u5e76\u6309\u56de\u8f66\u952e\u53d1\u9001\u6570\u636e\u7ed9\u670d\u52a1\u7aef\u3002<\/li>\n<li>\u5728\u670d\u52a1\u7aef\u7ec8\u7aef\u4e0a\uff0c\u53ef\u4ee5\u770b\u5230\u63a5\u6536\u5230\u7684\u6570\u636e\uff0c\u5e76\u53d1\u9001\u56de\u5ba2\u6237\u7aef\u3002<\/li>\n<li>\u6309 Ctrl + ] \u952e\u9000\u51fa telnet\u3002<\/li>\n<\/ol>\n<p>\u56db\u3001\u603b\u7ed3<br \/>\u901a\u8fc7\u4ee5\u4e0a\u4ee3\u7801\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230 Workerman \u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\uff0c\u5305\u62ec\u5b89\u88c5\u548c\u914d\u7f6e\u3001\u521b\u5efa\u670d\u52a1\u548c\u76d1\u542c\u7aef\u53e3\u3001\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42\u7b49\u3002\u501f\u52a9\u4e8e Workerman \u5f3a\u5927\u7684\u7f51\u7edc\u5904\u7406\u80fd\u529b\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u6784\u5efa\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u5927\u5bb6\u7406\u89e3\u548c\u4f7f\u7528 Workerman \u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5b9e\u73b0Workerman\u6587\u6863\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\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\u5b9e\u73b0 Workerman \u6587\u6863\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5 \u7b80\u4ecb\uff1aWorkerman \u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684PHP\u5f00\u53d1\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u8f7b\u677e\u6784\u5efa\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c06\u4ecb\u7ecd Workerman \u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\uff0c\u5305\u62ec\u5b89\u88c5\u548c\u914d\u7f6e\u3001\u521b\u5efa\u670d\u52a1\u548c\u76d1\u542c\u7aef\u53e3\u3001\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42\u7b49\u3002\u5e76\u7ed9\u51fa\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u5b89\u88c5\u548c\u914d\u7f6e Workerman \u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5 Workerman\uff1a composer require workerman\/workerman \u767b\u5f55\u540e\u590d\u5236 \u521b\u5efa\u4e00\u4e2a\u6587\u4ef6 server.php\uff0c\u5e76\u5bfc\u5165 WorkermanWorker\uff1a require_once __DIR__ . &#8216;\/vendor\/autoload.php&#8217;; use WorkermanWorker; \u767b\u5f55\u540e\u590d\u5236 \u914d\u7f6e Workerman \u7684\u8fd0\u884c\u53c2\u6570\uff1a $worker = new Worker(&#8216;tcp:\/\/0.0.0.0:1234&#8217;); $worker-&amp;gt;count = 4; $worker-&amp;gt;name = &#8216;MyWorker&#8217;; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0ctcp:\/\/0.0.0.0:1234 \u8868\u793a\u76d1\u542c\u672c\u5730\u7684 1234 \u7aef\u53e3\uff0ccount \u8868\u793a\u542f\u52a8 4 \u4e2a worker \u8fdb\u7a0b\uff0cname \u8868\u793a\u7ed9\u5f53\u524d worker \u8bbe\u7f6e\u4e00\u4e2a\u540d\u79f0\u3002 \u4e8c\u3001\u521b\u5efa\u670d\u52a1\u548c\u76d1\u542c\u7aef\u53e3 \u5728 server.php \u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u670d\u52a1\u5e76\u76d1\u542c\u7aef\u53e3\uff1a [&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-25033","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25033","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=25033"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25033\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}