{"id":24232,"date":"2024-11-21T09:00:21","date_gmt":"2024-11-21T01:00:21","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24232\/"},"modified":"2024-11-21T09:00:21","modified_gmt":"2024-11-21T01:00:21","slug":"%e4%bd%bf%e7%94%a8workerman%e5%bc%80%e5%8f%91%e9%ab%98%e6%80%a7%e8%83%bd%e7%9a%84%e7%89%a9%e6%b5%81%e9%85%8d%e9%80%81%e7%ae%a1%e7%90%86%e7%b3%bb%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24232\/","title":{"rendered":"\u4f7f\u7528Workerman\u5f00\u53d1\u9ad8\u6027\u80fd\u7684\u7269\u6d41\u914d\u9001\u7ba1\u7406\u7cfb\u7edf"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/465\/014\/169156000972877.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528Workerman\u5f00\u53d1\u9ad8\u6027\u80fd\u7684\u7269\u6d41\u914d\u9001\u7ba1\u7406\u7cfb\u7edf\u63d2\u56fe\" alt=\"\u4f7f\u7528Workerman\u5f00\u53d1\u9ad8\u6027\u80fd\u7684\u7269\u6d41\u914d\u9001\u7ba1\u7406\u7cfb\u7edf\u63d2\u56fe\" \/><\/p>\n<p>\u4f7f\u7528Workerman\u5f00\u53d1\u9ad8\u6027\u80fd\u7684\u7269\u6d41\u914d\u9001\u7ba1\u7406\u7cfb\u7edf<\/p>\n<p>\u968f\u7740\u7535\u5b50\u5546\u52a1\u884c\u4e1a\u7684\u8fc5\u731b\u53d1\u5c55\uff0c\u7269\u6d41\u914d\u9001\u7ba1\u7406\u7cfb\u7edf\u6210\u4e3a\u4e86\u5404\u5927\u4f01\u4e1a\u8ffd\u6c42\u9ad8\u6548\u8fd0\u8425\u7684\u5173\u952e\u3002\u4f20\u7edf\u7684\u7269\u6d41\u7cfb\u7edf\u5f80\u5f80\u9762\u4e34\u7740\u6027\u80fd\u4f4e\u4e0b\u3001\u5e76\u53d1\u5904\u7406\u80fd\u529b\u4e0d\u8db3\u7684\u95ee\u9898\uff0c\u800cWorkerman\u4f5c\u4e3a\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684PHP\u5f00\u53d1\u6846\u67b6\uff0c\u80fd\u591f\u5f88\u597d\u5730\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n<p>\u4e00\u3001Workerman\u7b80\u4ecb<\/p>\n<p>Workerman\u662f\u4e00\u6b3e\u57fa\u4e8ePHP\u7684\u5f00\u6e90\u9ad8\u6027\u80fd\u7684\u5f02\u6b65\u4e8b\u4ef6\u9a71\u52a8\u7684\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\u3002\u501f\u52a9\u4e8ePHP\u7684\u8fd0\u884c\u7279\u6027\uff0cWorkerman\u80fd\u591f\u5b9e\u73b0\u6210\u5343\u4e0a\u4e07\u7684\u5e76\u53d1\u8fde\u63a5\u5904\u7406\uff0c\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u7cfb\u7edf\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002<\/p>\n<p>\u4e8c\u3001\u642d\u5efa\u57fa\u7840\u670d\u52a1<\/p>\n<ol>\n<li>\u5b89\u88c5Workerman<\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u670d\u52a1\u5668\u4e0a\u5b89\u88c5Workerman\u3002\u4f7f\u7528composer\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<p>composer require \/workerman<\/p>\n<ol>\n<li>\u7f16\u5199\u57fa\u7840\u670d\u52a1<\/li>\n<\/ol>\n<p>\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u540d\u4e3a&#8221;server.php&#8221;\u7684\u6587\u4ef6\uff0c\u4f5c\u4e3a\u6211\u4eec\u7684\u57fa\u7840\u670d\u52a1\u3002<\/p>\n<pre>&lt;?php use WorkermanWorker;\n\nrequire_once __DIR__ . '\/vendor\/autoload.php';\n\n\/\/ \u521b\u5efa\u4e00\u4e2aWorker\u76d1\u542c9050\u7aef\u53e3\uff0c\u534f\u8bae\u4e3aTCP\n$worker = new Worker('tcp:\/\/0.0.0.0:9050');\n\n\/\/ \u542f\u52a8\u591a\u5c11\u4e2a\u8fdb\u7a0b\u6765\u5904\u7406\u8fde\u63a5\n$worker-&gt;count = 4;\n\n\/\/ \u5f53\u6709\u5ba2\u6237\u7aef\u8fde\u63a5\u65f6\u7684\u56de\u8c03\u51fd\u6570\n$worker-&amp;gt;onConnect = function($connection) {\n    echo \"New connection\n\";\n};\n\n\/\/ \u63a5\u6536\u5230\u5ba2\u6237\u7aef\u6570\u636e\u65f6\u7684\u56de\u8c03\u51fd\u6570\n$worker-&amp;gt;onMessage = function($connection, $data) {\n    \/\/ \u8fd9\u91cc\u53ef\u4ee5\u5904\u7406\u63a5\u6536\u5230\u7684\u6570\u636e\n    $connection-&amp;gt;send('Hello, ' . $data);\n};\n\n\/\/ \u5f53\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5\u65f6\u7684\u56de\u8c03\u51fd\u6570\n$worker-&amp;gt;onClose = function($connection) {\n    echo \"Connection closed\n\";\n};\n\n\/\/ \u8fd0\u884cworker\nWorker::runAll();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e09\u3001\u5b9e\u73b0\u7269\u6d41\u914d\u9001\u7ba1\u7406\u7cfb\u7edf<\/p>\n<p>\u5728\u642d\u5efa\u597d\u57fa\u7840\u670d\u52a1\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u5b9e\u73b0\u7269\u6d41\u914d\u9001\u7ba1\u7406\u7cfb\u7edf\u3002<\/p>\n<ol>\n<li>\u7528\u6237\u4e0b\u5355<\/li>\n<\/ol>\n<p>\u5f53\u7528\u6237\u4e0b\u5355\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8ba2\u5355\u63d0\u4ea4\u5230\u914d\u9001\u7ba1\u7406\u7cfb\u7edf\uff0c\u53ef\u4ee5\u4f7f\u7528HTTP\u534f\u8bae\u5c06\u6570\u636e\u4f20\u8f93\u5230\u6211\u4eec\u7684\u57fa\u7840\u670d\u52a1\u3002<\/p>\n<pre>&lt;?php $order = [\n    'order_id' =&gt; 1,\n    'user_id' =&amp;gt; 123,\n    \/\/ \u5176\u4ed6\u8ba2\u5355\u4fe1\u606f...\n];\n\n$client = new GuzzleHttpClient();\n$response = $client-&amp;gt;request('POST', 'http:\/\/your_server_ip:9050', [\n    'json' =&amp;gt; $order,\n]);\n\necho $response-&amp;gt;getBody();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u7cfb\u7edf\u5904\u7406\u8ba2\u5355<\/li>\n<\/ol>\n<p>\u5728\u57fa\u7840\u670d\u52a1\u7684&#8221;onMessage&#8221;\u56de\u8c03\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u63a5\u6536\u5230\u7684\u8ba2\u5355\u8fdb\u884c\u5904\u7406\uff0c\u7136\u540e\u8fd4\u56de\u5904\u7406\u7ed3\u679c\u7ed9\u7528\u6237\u3002<\/p>\n<pre>$worker-&amp;gt;onMessage = function($connection, $data) {\n    \/\/ \u63a5\u6536\u5230\u7528\u6237\u8ba2\u5355\n    $order = json_decode($data, true);\n    \n    \/\/ \u5904\u7406\u8ba2\u5355...\n    \n    \/\/ \u8fd4\u56de\u5904\u7406\u7ed3\u679c\u7ed9\u7528\u6237\n    $connection-&amp;gt;send('Order processed');\n};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u642d\u5efa\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u7269\u6d41\u914d\u9001\u7ba1\u7406\u7cfb\u7edf\uff0c\u5177\u5907\u5f3a\u5927\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>Workerman\u662f\u4e00\u6b3e\u5f3a\u5927\u7684PHP\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u901a\u8fc7\u5176\u9ad8\u6027\u80fd\u7684\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u7269\u6d41\u914d\u9001\u7ba1\u7406\u7cfb\u7edf\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8fdb\u884c\u529f\u80fd\u6269\u5c55\uff0c\u4f8b\u5982\u6dfb\u52a0\u8ba2\u5355\u67e5\u8be2\u3001\u7269\u6d41\u8ddf\u8e2a\u7b49\u529f\u80fd\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u4e86\u89e3\u548c\u4f7f\u7528Workerman\u5f00\u53d1\u7269\u6d41\u914d\u9001\u7ba1\u7406\u7cfb\u7edf\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528Workerman\u5f00\u53d1\u9ad8\u6027\u80fd\u7684\u7269\u6d41\u914d\u9001\u7ba1\u7406\u7cfb\u7edf\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>\u4f7f\u7528Workerman\u5f00\u53d1\u9ad8\u6027\u80fd\u7684\u7269\u6d41\u914d\u9001\u7ba1\u7406\u7cfb\u7edf \u968f\u7740\u7535\u5b50\u5546\u52a1\u884c\u4e1a\u7684\u8fc5\u731b\u53d1\u5c55\uff0c\u7269\u6d41\u914d\u9001\u7ba1\u7406\u7cfb\u7edf\u6210\u4e3a\u4e86\u5404\u5927\u4f01\u4e1a\u8ffd\u6c42\u9ad8\u6548\u8fd0\u8425\u7684\u5173\u952e\u3002\u4f20\u7edf\u7684\u7269\u6d41\u7cfb\u7edf\u5f80\u5f80\u9762\u4e34\u7740\u6027\u80fd\u4f4e\u4e0b\u3001\u5e76\u53d1\u5904\u7406\u80fd\u529b\u4e0d\u8db3\u7684\u95ee\u9898\uff0c\u800cWorkerman\u4f5c\u4e3a\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684PHP\u5f00\u53d1\u6846\u67b6\uff0c\u80fd\u591f\u5f88\u597d\u5730\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002 \u4e00\u3001Workerman\u7b80\u4ecb Workerman\u662f\u4e00\u6b3e\u57fa\u4e8ePHP\u7684\u5f00\u6e90\u9ad8\u6027\u80fd\u7684\u5f02\u6b65\u4e8b\u4ef6\u9a71\u52a8\u7684\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\u3002\u501f\u52a9\u4e8ePHP\u7684\u8fd0\u884c\u7279\u6027\uff0cWorkerman\u80fd\u591f\u5b9e\u73b0\u6210\u5343\u4e0a\u4e07\u7684\u5e76\u53d1\u8fde\u63a5\u5904\u7406\uff0c\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u7cfb\u7edf\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002 \u4e8c\u3001\u642d\u5efa\u57fa\u7840\u670d\u52a1 \u5b89\u88c5Workerman \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u670d\u52a1\u5668\u4e0a\u5b89\u88c5Workerman\u3002\u4f7f\u7528composer\u8fdb\u884c\u5b89\u88c5\uff1a composer require \/workerman \u7f16\u5199\u57fa\u7840\u670d\u52a1 \u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u540d\u4e3a&#8221;server.php&#8221;\u7684\u6587\u4ef6\uff0c\u4f5c\u4e3a\u6211\u4eec\u7684\u57fa\u7840\u670d\u52a1\u3002 &lt;?php use WorkermanWorker; require_once __DIR__ . &#8216;\/vendor\/autoload.php&#8217;; \/\/ \u521b\u5efa\u4e00\u4e2aWorker\u76d1\u542c9050\u7aef\u53e3\uff0c\u534f\u8bae\u4e3aTCP $worker = new Worker(&#8216;tcp:\/\/0.0.0.0:9050&#8217;); \/\/ \u542f\u52a8\u591a\u5c11\u4e2a\u8fdb\u7a0b\u6765\u5904\u7406\u8fde\u63a5 $worker-&gt;count = 4; \/\/ \u5f53\u6709\u5ba2\u6237\u7aef\u8fde\u63a5\u65f6\u7684\u56de\u8c03\u51fd\u6570 $worker-&amp;gt;onConnect = function($connection) { echo &#8220;New connection &#8220;; }; \/\/ \u63a5\u6536\u5230\u5ba2\u6237\u7aef\u6570\u636e\u65f6\u7684\u56de\u8c03\u51fd\u6570 $worker-&amp;gt;onMessage = function($connection, $data) { \/\/ \u8fd9\u91cc\u53ef\u4ee5\u5904\u7406\u63a5\u6536\u5230\u7684\u6570\u636e $connection-&amp;gt;send(&#8216;Hello, &#8216; . $data); }; [&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-24232","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24232","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=24232"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24232\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}