{"id":24450,"date":"2024-11-21T09:31:20","date_gmt":"2024-11-21T01:31:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24450\/"},"modified":"2024-11-21T09:31:20","modified_gmt":"2024-11-21T01:31:20","slug":"workerman%e5%bc%80%e6%94%be%e6%a1%86%e6%9e%b6%e5%8e%9f%e7%90%86%e8%a7%a3%e6%9e%90%ef%bc%9a%e6%8e%a2%e7%b4%a2%e5%85%b6%e9%ab%98%e6%80%a7%e8%83%bd%e7%9a%84%e7%a7%98%e5%af%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24450\/","title":{"rendered":"Workerman\u5f00\u653e\u6846\u67b6\u539f\u7406\u89e3\u6790\uff1a\u63a2\u7d22\u5176\u9ad8\u6027\u80fd\u7684\u79d8\u5bc6"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169294609647397.jpg\" class=\"aligncenter\" title=\"Workerman\u5f00\u653e\u6846\u67b6\u539f\u7406\u89e3\u6790\uff1a\u63a2\u7d22\u5176\u9ad8\u6027\u80fd\u7684\u79d8\u5bc6\u63d2\u56fe\" alt=\"Workerman\u5f00\u653e\u6846\u67b6\u539f\u7406\u89e3\u6790\uff1a\u63a2\u7d22\u5176\u9ad8\u6027\u80fd\u7684\u79d8\u5bc6\u63d2\u56fe\" \/><\/p>\n<p>Workerman\u5f00\u653e\u6846\u67b6\u539f\u7406\u89e3\u6790\uff1a\u63a2\u7d22\u5176\u9ad8\u6027\u80fd\u7684\u79d8\u5bc6<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u5728\u5f53\u4eca\u7684\u4e92\u8054\u7f51\u65f6\u4ee3\uff0c\u5bf9\u4e8e\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u6784\u5efa\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u5e94\u7528\u662f\u4e00\u9879\u91cd\u8981\u7684\u4efb\u52a1\u3002\u800cWorkerman\u5f00\u653e\u6846\u67b6\u5219\u662f\u4e00\u4e2a\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u7684\u89e3\u51b3\u65b9\u6848\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u89e3\u6790Workerman\u6846\u67b6\u7684\u539f\u7406\uff0c\u63a2\u7d22\u5176\u9ad8\u6027\u80fd\u7684\u79d8\u5bc6\u3002<\/p>\n<p>\u4e00\u3001Workerman\u6846\u67b6\u7b80\u4ecb<br \/>Workerman\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684PHP\u5f00\u653e\u6846\u67b6\uff0c\u5b83\u901a\u8fc7\u4f7f\u7528\u539f\u751f\u7684PHP Socket\u6269\u5c55\uff0c\u4f7f\u5f97PHP\u80fd\u591f\u652f\u6301\u591a\u8fdb\u7a0b\u3001\u591a\u7ebf\u7a0b\u3001\u5f02\u6b65\u7b49\u7279\u6027\u3002Workerman\u6846\u67b6\u5177\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a<\/p>\n<ol>\n<li>\u5feb\u901f\u3001\u9ad8\u6027\u80fd\uff1aWorkerman\u91c7\u7528\u4e86\u975e\u963b\u585eI\/O\u64cd\u4f5c\uff0c\u901a\u8fc7\u4e8b\u4ef6\u8f6e\u8be2\u673a\u5236\u6765\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42\uff0c\u4ece\u800c\u5b9e\u73b0\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u901a\u4fe1\u3002<\/li>\n<li>\u591a\u8fdb\u7a0b\u3001\u591a\u7ebf\u7a0b\uff1aWorkerman\u6846\u67b6\u652f\u6301\u591a\u8fdb\u7a0b\u6a21\u5f0f\u548c\u591a\u7ebf\u7a0b\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5145\u5206\u5229\u7528\u673a\u5668\u7684\u591a\u6838\u5904\u7406\u5668\u8d44\u6e90\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002<\/li>\n<li>\u5f02\u6b65\u7f16\u7a0b\uff1aWorkerman\u91c7\u7528\u5f02\u6b65\u7f16\u7a0b\u6a21\u5f0f\uff0c\u5c06I\/O\u64cd\u4f5c\u4ea4\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u5904\u7406\uff0c\u51cf\u5c11\u4e86CPU\u7684\u7a7a\u95f2\u65f6\u95f4\uff0c\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002<\/li>\n<\/ol>\n<p>\u4e8c\u3001Workerman\u6846\u67b6\u7684\u4e3b\u8981\u539f\u7406<\/p>\n<ol>\n<li>Socket\u901a\u4fe1\uff1aWorkerman\u6846\u67b6\u57fa\u4e8e\u539f\u751f\u7684PHP Socket\u6269\u5c55\uff0c\u4f7f\u7528TCP\/IP\u534f\u8bae\u8fdb\u884c\u7f51\u7edc\u901a\u4fe1\u3002\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u76d1\u542cSocket\uff0c\u63a5\u6536\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\uff0c\u5e76\u7ba1\u7406\u8fde\u63a5\u7684\u5efa\u7acb\u548c\u5173\u95ed\u3002<\/li>\n<li>\u591a\u8fdb\u7a0b\/\u591a\u7ebf\u7a0b\u6a21\u5f0f\uff1aWorkerman\u6846\u67b6\u652f\u6301\u591a\u8fdb\u7a0b\u548c\u591a\u7ebf\u7a0b\u4e24\u79cd\u6a21\u5f0f\u3002\u5728\u591a\u8fdb\u7a0b\u6a21\u5f0f\u4e0b\uff0cWorker\u8fdb\u7a0b\u91c7\u7528Master-Worker\u6a21\u5f0f\uff0cMaster\u8fdb\u7a0b\u76d1\u542cSocket\u5e76\u63a5\u6536\u5ba2\u6237\u7aef\u8bf7\u6c42\uff0c\u7136\u540e\u5c06\u8bf7\u6c42\u5206\u53d1\u7ed9Worker\u8fdb\u7a0b\u8fdb\u884c\u5904\u7406\u3002\u800c\u5728\u591a\u7ebf\u7a0b\u6a21\u5f0f\u4e0b\uff0c\u6bcf\u4e2aWorker\u7ebf\u7a0b\u90fd\u72ec\u7acb\u76d1\u542cSocket\u5e76\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42\u3002<\/li>\n<li>\u4e8b\u4ef6\u8f6e\u8be2\uff1aWorkerman\u6846\u67b6\u91c7\u7528\u4e8b\u4ef6\u8f6e\u8be2\u673a\u5236\uff0c\u901a\u8fc7\u975e\u963b\u585eI\/O\u64cd\u4f5c\u548c\u4e8b\u4ef6\u9a71\u52a8\u7684\u65b9\u5f0f\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42\u3002\u5f53\u6709\u65b0\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u8bf7\u6c42\u5230\u8fbe\u65f6\uff0c\u901a\u8fc7\u4e8b\u4ef6\u56de\u8c03\u7684\u65b9\u5f0f\u89e6\u53d1\u5bf9\u5e94\u7684\u5904\u7406\u903b\u8f91\u3002\u800c\u5bf9\u4e8e\u5df2\u5efa\u7acb\u7684\u8fde\u63a5\uff0cWorkerman\u6846\u67b6\u7ef4\u62a4\u4e86\u4e00\u4e2a\u4e8b\u4ef6\u5faa\u73af\uff0c\u4e0d\u65ad\u5730\u68c0\u6d4bSocket\u662f\u5426\u6709\u65b0\u7684\u6570\u636e\u5230\u8fbe\uff0c\u82e5\u6709\u5219\u89e6\u53d1\u76f8\u5e94\u7684\u4e8b\u4ef6\u56de\u8c03\u3002<\/li>\n<li>\u5f02\u6b65\u7f16\u7a0b\uff1aWorkerman\u6846\u67b6\u91c7\u7528\u5f02\u6b65\u7f16\u7a0b\u6a21\u5f0f\uff0c\u901a\u8fc7\u4f7f\u7528PHP\u7684\u7279\u6027\u5982yield\u5173\u952e\u5b57\u548cGenerator\u6765\u5b9e\u73b0\u5f02\u6b65\u8c03\u7528\u3002\u5f02\u6b65\u7f16\u7a0b\u80fd\u591f\u907f\u514dCPU\u5728\u7b49\u5f85I\/O\u64cd\u4f5c\u5b8c\u6210\u65f6\u7684\u7a7a\u95f2\uff0c\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002<\/li>\n<\/ol>\n<p>\u4e09\u3001Workerman\u6846\u67b6\u793a\u4f8b\u4ee3\u7801<br \/>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Workerman\u6846\u67b6\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u804a\u5929\u5ba4\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p>use WorkermanWorker;<\/p>\n<p>require_once <strong>DIR<\/strong> . &#8216;\/vendor\/autoload.php&#8217;;<\/p>\n<p>\/\/ \u521b\u5efa\u4e00\u4e2aWorker\u76d1\u542c8090\u7aef\u53e3\uff0c\u4f7f\u7528websocket\u534f\u8bae\u8fdb\u884c\u901a\u4fe1<br \/>$worker = new Worker(&#8216;websocket:\/\/0.0.0.0:8090&#8217;);<\/p>\n<p>\/\/ \u542f\u52a84\u4e2a\u8fdb\u7a0b\uff0c\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42<br \/>$worker-&gt;count = 4;<\/p>\n<p>\/\/ \u5f53\u5ba2\u6237\u7aef\u8fde\u63a5\u5efa\u7acb\u65f6\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>$worker-&gt;onConnect = function ($connection) {<\/p>\n<pre>echo \"New connection<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>&#8220;;<br \/>};<\/p>\n<p>\/\/ \u5f53\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5\u65f6\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>$worker-&gt;onClose = function ($connection) {<\/p>\n<pre>echo \"Connection closed<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>&#8220;;<br \/>};<\/p>\n<p>\/\/ \u5f53\u5ba2\u6237\u7aef\u53d1\u9001\u6d88\u606f\u5230\u8fbe\u65f6\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570<br \/>$worker-&gt;onMessage = function ($connection, $message) {<\/p>\n<pre>\/\/ \u5e7f\u64ad\u6d88\u606f\u7ed9\u6240\u6709\u8fde\u63a5\u7684\u5ba2\u6237\u7aef\nforeach ($worker-&amp;gt;connections as $client) {\n    $client-&amp;gt;send($message);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>};<\/p>\n<p>\/\/ \u8fd0\u884cworker<br \/>Worker::runAll();<\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u793a\u4f8b\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4f7f\u7528Workerman\u6846\u67b6\u521b\u5efa\u4e00\u4e2a\u804a\u5929\u5ba4\u7684\u8fc7\u7a0b\u975e\u5e38\u7b80\u6d01\u548c\u65b9\u4fbf\u3002Worker\u7c7b\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56de\u8c03\u51fd\u6570\uff0c\u53ef\u4ee5\u9488\u5bf9\u4e0d\u540c\u7684\u4e8b\u4ef6\u8fdb\u884c\u5904\u7406\uff0c\u4f7f\u5f97\u5f00\u53d1\u7f51\u7edc\u5e94\u7528\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u3002<\/p>\n<p>\u7ed3\u8bba\uff1a<br \/>Workerman\u6846\u67b6\u662f\u4e00\u4e2a\u4f18\u79c0\u7684\u5f00\u653e\u6846\u67b6\uff0c\u5177\u5907\u9ad8\u6027\u80fd\u3001\u591a\u8fdb\u7a0b\/\u591a\u7ebf\u7a0b\u6a21\u5f0f\u3001\u5f02\u6b65\u7f16\u7a0b\u7b49\u7279\u70b9\u3002\u5b83\u7684\u539f\u7406\u662f\u57fa\u4e8e\u539f\u751f\u7684PHP Socket\u6269\u5c55\uff0c\u901a\u8fc7\u4e8b\u4ef6\u8f6e\u8be2\u673a\u5236\u548c\u5f02\u6b65\u7f16\u7a0b\u6a21\u5f0f\u6765\u5b9e\u73b0\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u901a\u4fe1\u3002\u901a\u8fc7\u672c\u6587\u7684\u539f\u7406\u89e3\u6790\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u5e0c\u671b\u8bfb\u8005\u80fd\u591f\u5bf9Workerman\u6846\u67b6\u6709\u66f4\u6df1\u5165\u7684\u7406\u89e3\uff0c\u5e76\u80fd\u591f\u7075\u6d3b\u5e94\u7528\u4e8e\u5f00\u53d1\u5b9e\u8df5\u4e2d\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fWorkerman\u5f00\u653e\u6846\u67b6\u539f\u7406\u89e3\u6790\uff1a\u63a2\u7d22\u5176\u9ad8\u6027\u80fd\u7684\u79d8\u5bc6\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>Workerman\u5f00\u653e\u6846\u67b6\u539f\u7406\u89e3\u6790\uff1a\u63a2\u7d22\u5176\u9ad8\u6027\u80fd\u7684\u79d8\u5bc6 \u5f15\u8a00\uff1a\u5728\u5f53\u4eca\u7684\u4e92\u8054\u7f51\u65f6\u4ee3\uff0c\u5bf9\u4e8e\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u6784\u5efa\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u5e94\u7528\u662f\u4e00\u9879\u91cd\u8981\u7684\u4efb\u52a1\u3002\u800cWorkerman\u5f00\u653e\u6846\u67b6\u5219\u662f\u4e00\u4e2a\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u7684\u89e3\u51b3\u65b9\u6848\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u89e3\u6790Workerman\u6846\u67b6\u7684\u539f\u7406\uff0c\u63a2\u7d22\u5176\u9ad8\u6027\u80fd\u7684\u79d8\u5bc6\u3002 \u4e00\u3001Workerman\u6846\u67b6\u7b80\u4ecbWorkerman\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684PHP\u5f00\u653e\u6846\u67b6\uff0c\u5b83\u901a\u8fc7\u4f7f\u7528\u539f\u751f\u7684PHP Socket\u6269\u5c55\uff0c\u4f7f\u5f97PHP\u80fd\u591f\u652f\u6301\u591a\u8fdb\u7a0b\u3001\u591a\u7ebf\u7a0b\u3001\u5f02\u6b65\u7b49\u7279\u6027\u3002Workerman\u6846\u67b6\u5177\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a \u5feb\u901f\u3001\u9ad8\u6027\u80fd\uff1aWorkerman\u91c7\u7528\u4e86\u975e\u963b\u585eI\/O\u64cd\u4f5c\uff0c\u901a\u8fc7\u4e8b\u4ef6\u8f6e\u8be2\u673a\u5236\u6765\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42\uff0c\u4ece\u800c\u5b9e\u73b0\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u901a\u4fe1\u3002 \u591a\u8fdb\u7a0b\u3001\u591a\u7ebf\u7a0b\uff1aWorkerman\u6846\u67b6\u652f\u6301\u591a\u8fdb\u7a0b\u6a21\u5f0f\u548c\u591a\u7ebf\u7a0b\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5145\u5206\u5229\u7528\u673a\u5668\u7684\u591a\u6838\u5904\u7406\u5668\u8d44\u6e90\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002 \u5f02\u6b65\u7f16\u7a0b\uff1aWorkerman\u91c7\u7528\u5f02\u6b65\u7f16\u7a0b\u6a21\u5f0f\uff0c\u5c06I\/O\u64cd\u4f5c\u4ea4\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u5904\u7406\uff0c\u51cf\u5c11\u4e86CPU\u7684\u7a7a\u95f2\u65f6\u95f4\uff0c\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002 \u4e8c\u3001Workerman\u6846\u67b6\u7684\u4e3b\u8981\u539f\u7406 Socket\u901a\u4fe1\uff1aWorkerman\u6846\u67b6\u57fa\u4e8e\u539f\u751f\u7684PHP Socket\u6269\u5c55\uff0c\u4f7f\u7528TCP\/IP\u534f\u8bae\u8fdb\u884c\u7f51\u7edc\u901a\u4fe1\u3002\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u76d1\u542cSocket\uff0c\u63a5\u6536\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\uff0c\u5e76\u7ba1\u7406\u8fde\u63a5\u7684\u5efa\u7acb\u548c\u5173\u95ed\u3002 \u591a\u8fdb\u7a0b\/\u591a\u7ebf\u7a0b\u6a21\u5f0f\uff1aWorkerman\u6846\u67b6\u652f\u6301\u591a\u8fdb\u7a0b\u548c\u591a\u7ebf\u7a0b\u4e24\u79cd\u6a21\u5f0f\u3002\u5728\u591a\u8fdb\u7a0b\u6a21\u5f0f\u4e0b\uff0cWorker\u8fdb\u7a0b\u91c7\u7528Master-Worker\u6a21\u5f0f\uff0cMaster\u8fdb\u7a0b\u76d1\u542cSocket\u5e76\u63a5\u6536\u5ba2\u6237\u7aef\u8bf7\u6c42\uff0c\u7136\u540e\u5c06\u8bf7\u6c42\u5206\u53d1\u7ed9Worker\u8fdb\u7a0b\u8fdb\u884c\u5904\u7406\u3002\u800c\u5728\u591a\u7ebf\u7a0b\u6a21\u5f0f\u4e0b\uff0c\u6bcf\u4e2aWorker\u7ebf\u7a0b\u90fd\u72ec\u7acb\u76d1\u542cSocket\u5e76\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42\u3002 \u4e8b\u4ef6\u8f6e\u8be2\uff1aWorkerman\u6846\u67b6\u91c7\u7528\u4e8b\u4ef6\u8f6e\u8be2\u673a\u5236\uff0c\u901a\u8fc7\u975e\u963b\u585eI\/O\u64cd\u4f5c\u548c\u4e8b\u4ef6\u9a71\u52a8\u7684\u65b9\u5f0f\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42\u3002\u5f53\u6709\u65b0\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u8bf7\u6c42\u5230\u8fbe\u65f6\uff0c\u901a\u8fc7\u4e8b\u4ef6\u56de\u8c03\u7684\u65b9\u5f0f\u89e6\u53d1\u5bf9\u5e94\u7684\u5904\u7406\u903b\u8f91\u3002\u800c\u5bf9\u4e8e\u5df2\u5efa\u7acb\u7684\u8fde\u63a5\uff0cWorkerman\u6846\u67b6\u7ef4\u62a4\u4e86\u4e00\u4e2a\u4e8b\u4ef6\u5faa\u73af\uff0c\u4e0d\u65ad\u5730\u68c0\u6d4bSocket\u662f\u5426\u6709\u65b0\u7684\u6570\u636e\u5230\u8fbe\uff0c\u82e5\u6709\u5219\u89e6\u53d1\u76f8\u5e94\u7684\u4e8b\u4ef6\u56de\u8c03\u3002 \u5f02\u6b65\u7f16\u7a0b\uff1aWorkerman\u6846\u67b6\u91c7\u7528\u5f02\u6b65\u7f16\u7a0b\u6a21\u5f0f\uff0c\u901a\u8fc7\u4f7f\u7528PHP\u7684\u7279\u6027\u5982yield\u5173\u952e\u5b57\u548cGenerator\u6765\u5b9e\u73b0\u5f02\u6b65\u8c03\u7528\u3002\u5f02\u6b65\u7f16\u7a0b\u80fd\u591f\u907f\u514dCPU\u5728\u7b49\u5f85I\/O\u64cd\u4f5c\u5b8c\u6210\u65f6\u7684\u7a7a\u95f2\uff0c\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002 \u4e09\u3001Workerman\u6846\u67b6\u793a\u4f8b\u4ee3\u7801\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Workerman\u6846\u67b6\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u804a\u5929\u5ba4\u7684\u793a\u4f8b\u4ee3\u7801\uff1a use WorkermanWorker; require_once DIR . &#8216;\/vendor\/autoload.php&#8217;; \/\/ \u521b\u5efa\u4e00\u4e2aWorker\u76d1\u542c8090\u7aef\u53e3\uff0c\u4f7f\u7528websocket\u534f\u8bae\u8fdb\u884c\u901a\u4fe1$worker = new Worker(&#8216;websocket:\/\/0.0.0.0:8090&#8217;); \/\/ \u542f\u52a84\u4e2a\u8fdb\u7a0b\uff0c\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42$worker-&gt;count = 4; \/\/ \u5f53\u5ba2\u6237\u7aef\u8fde\u63a5\u5efa\u7acb\u65f6\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570$worker-&gt;onConnect = function ($connection) { echo &#8220;New connection \u767b\u5f55\u540e\u590d\u5236 &#8220;;}; \/\/ \u5f53\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5\u65f6\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570$worker-&gt;onClose = function ($connection) { echo &#8220;Connection closed \u767b\u5f55\u540e\u590d\u5236 &#8220;;}; \/\/ \u5f53\u5ba2\u6237\u7aef\u53d1\u9001\u6d88\u606f\u5230\u8fbe\u65f6\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570$worker-&gt;onMessage = function [&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-24450","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24450","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=24450"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24450\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}