{"id":23564,"date":"2024-11-21T13:09:38","date_gmt":"2024-11-21T05:09:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23564\/"},"modified":"2024-11-21T13:09:38","modified_gmt":"2024-11-21T05:09:38","slug":"swoole%e8%bf%9b%e9%98%b6%ef%bc%9a%e4%bd%bf%e7%94%a8%e5%8d%8f%e7%a8%8b%e7%bc%96%e5%86%99%e5%b9%b6%e5%8f%91%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23564\/","title":{"rendered":"Swoole\u8fdb\u9636\uff1a\u4f7f\u7528\u534f\u7a0b\u7f16\u5199\u5e76\u53d1\u670d\u52a1\u5668"},"content":{"rendered":"<p>\u5728\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u4f5c\u4e3a\u4e00\u6b3e\u957f\u7a0b\u8fdb\u7a0b\u901a\u4fe1\u6846\u67b6\uff0c\u8d8a\u6765\u8d8a\u53d7\u5230\u5f00\u53d1\u8005\u7684\u9752\u7750\u3002swoole\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7f51\u7edc\u7f16\u7a0bapi\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u5f02\u6b65\u7f16\u7a0b\uff0c\u63d0\u9ad8\u4e86\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528swoole\u548c\u534f\u7a0b\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u5e76\u53d1\u670d\u52a1\u5668\u3002<\/p>\n<p>\u4e00\u3001\u73af\u5883\u642d\u5efa<\/p>\n<p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Swoole\u6269\u5c55\uff0c\u5b89\u88c5\u65b9\u6cd5\u53ef\u4ee5\u53c2\u8003Swoole\u5b98\u65b9\u6587\u6863\u3002\u672c\u6587\u4f7f\u7528\u7684\u662fPHP7.2\u7248\u672c\u3002<\/p>\n<p>\u4e8c\u3001\u670d\u52a1\u5668\u7a0b\u5e8f\u6846\u67b6<\/p>\n<p>\u6211\u4eec\u9700\u8981\u4f7f\u7528Swoole\u7684TCP\u670d\u52a1\u5668\uff0c\u5177\u4f53\u5b9e\u73b0\u9700\u8981\u8003\u8651\u5982\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<\/p>\n<ol>\n<li>\u5b9a\u4e49\u534f\u8bae\u683c\u5f0f<\/li>\n<\/ol>\n<p>\u5728\u7f51\u7edc\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u5b9a\u4e49\u4e00\u79cd\u6807\u51c6\u7684\u6570\u636e\u4f20\u8f93\u683c\u5f0f\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u534f\u8bae\u683c\u5f0f\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>class MyProtocol {\n    const HEADER_SIZE = 4;\n    const MAX_PACKAGE_SIZE = 1024 * 1024;\n\n    public static function encode($data) {\n        $package = json_encode($data, JSON_UNESCAPED_UNICODE);\n        return pack('N', strlen($package)) . $package;\n    }\n\n    public static function decode($buffer) {\n        if(strlen($buffer)  self::MAX_PACKAGE_SIZE) {\n            return false;\n        }\n\n        if(strlen($buffer) &lt;p&gt;\u534f\u8bae\u683c\u5f0f\u5305\u542b\u4e00\u4e2a4\u5b57\u8282\u7684\u5934\u90e8\uff0c\u7528\u4e8e\u5b58\u653e\u6570\u636e\u5305\u7684\u957f\u5ea6\uff0c\u548c\u4e00\u4e2aJSON\u5b57\u7b26\u4e32\u8868\u793a\u7684\u5b9e\u9645\u6570\u636e\u3002\u8fd9\u79cd\u683c\u5f0f\u53ef\u4ee5\u652f\u6301\u4e0d\u540c\u7684\u6d88\u606f\u7c7b\u578b\uff0c\u5e76\u5b9e\u73b0\u4f20\u8f93\u7684\u53ef\u9760\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002&lt;\/p&gt;&lt;ol start=\"2\"&gt;&lt;li&gt;\u5b9a\u4e49\u4e1a\u52a1\u5904\u7406&lt;\/li&gt;&lt;\/ol&gt;&lt;p&gt;Server\u7c7b\u7684\u56de\u8c03\u51fd\u6570\u4e2d\u5b9a\u4e49\u4e1a\u52a1\u903b\u8f91\u5904\u7406\uff0c\u5982\u4e0b\u6240\u793a\uff1a&lt;\/p&gt;&lt;pre class=\"brush:php;toolbar:false;\"&gt;class Server {\n    private $serv;\n\n    public function __construct() {\n        $this-&amp;gt;serv = new SwooleServer('0.0.0.0', 9501);\n        $this-&amp;gt;serv-&amp;gt;set(array(\n            'worker_num' =&amp;gt; 4,\n            'daemonize' =&amp;gt; false,\n            'max_conn' =&amp;gt; 10000,\n            'dispatch_mode' =&amp;gt; 3,\n            'open_tcp_keepalive' =&amp;gt; 1,\n            'tcp_keepidle' =&amp;gt; 600,\n            'tcp_keepinterval' =&amp;gt; 60,\n            'tcp_keepcount' =&amp;gt; 5,\n        ));\n        $this-&amp;gt;serv-&amp;gt;on('Connect', array($this, 'onConnect'));\n        $this-&amp;gt;serv-&amp;gt;on('Receive', array($this, 'onReceive'));\n        $this-&amp;gt;serv-&amp;gt;on('Close', array($this, 'onClose'));\n        $this-&amp;gt;serv-&amp;gt;start();\n    }\n\n    public function onConnect($serv, $fd, $reactorId) {\n        echo \"Client: {$fd}-{$reactorId} Connect.\n\";\n    }\n\n    public function onReceive($serv, $fd, $reactorId, $data) {\n        $message = MyProtocol::decode($data);\n        if($message) {\n            \/\/ Handle message &amp;amp; reply to client\n            $this-&amp;gt;serv-&amp;gt;send($fd, MyProtocol::encode(array('status' =&amp;gt; 0, 'message' =&amp;gt; 'OK')));\n        } else {\n            \/\/ Invalid message, close connection\n            $this-&amp;gt;serv-&amp;gt;close($fd);\n        }\n    }\n\n    public function onClose($serv, $fd, $reactorId) {\n        echo \"Client: {$fd}-{$reactorId} Close.\n\";\n    }\n}\n\nnew Server();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5bf9\u4e8e\u6bcf\u4e2a\u8fde\u63a5\uff0c\u670d\u52a1\u5668\u9700\u8981\u5b9a\u4e49\u4e09\u4e2a\u65b9\u6cd5\u5904\u7406\u5176\u8fde\u63a5\u3001\u63a5\u53d7\u6d88\u606f\u3001\u5173\u95ed\u8fde\u63a5\u7b49\u64cd\u4f5c\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u54cd\u5e94\u3002<\/p>\n<p>\u4e09\u3001\u4f7f\u7528\u534f\u7a0b<\/p>\n<p>Swoole\u63d0\u4f9b\u4e86\u534f\u7a0bAPI\uff0c\u7528\u6765\u7ba1\u7406\u5f02\u6b65\u7f16\u7a0b\u4e2d\u7684\u63a7\u5236\u6d41\uff0c\u63d0\u4f9b\u7c7b\u4f3c\u4e8e\u540c\u6b65\u7684\u7f16\u7a0b\u4f53\u9a8c\u3002\u53ef\u4ee5\u901a\u8fc7coroutine\u7cfb\u5217API\u6765\u5b9e\u73b0\u534f\u7a0b\u529f\u80fd\u3002\u4e0b\u9762\u662f\u4f7f\u7528\u534f\u7a0b\u540e\u7684\u65b0\u4ee3\u7801\uff0c\u4f7f\u7528\u4e86\u534f\u7a0b\u6765\u5904\u7406\u5ba2\u6237\u7aef\u8fde\u63a5\u548c\u6d88\u606f\u63a5\u6536\u7b49\u5f02\u6b65IO\u64cd\u4f5c\uff1a<\/p>\n<pre>class Server {\n    private $serv;\n\n    public function __construct() {\n        $this-&amp;gt;serv = new SwooleServer('0.0.0.0', 9501);\n        $this-&amp;gt;serv-&amp;gt;set(array(\n            'worker_num' =&amp;gt; 4,\n            'daemonize' =&amp;gt; false,\n            'max_conn' =&amp;gt; 10000,\n            'dispatch_mode' =&amp;gt; 3,\n            'open_tcp_keepalive' =&amp;gt; 1,\n            'tcp_keepidle' =&amp;gt; 600,\n            'tcp_keepinterval' =&amp;gt; 60,\n            'tcp_keepcount' =&amp;gt; 5,\n        ));\n        $this-&amp;gt;serv-&amp;gt;on('Connect', array($this, 'onConnect'));\n        $this-&amp;gt;serv-&amp;gt;on('Receive', array($this, 'onReceive'));\n        $this-&amp;gt;serv-&amp;gt;on('Close', array($this, 'onClose'));\n        $this-&amp;gt;serv-&amp;gt;start();\n    }\n\n    public function onConnect($serv, $fd, $reactorId) {\n        go(function() use($fd, $reactorId) {\n            echo \"Client: {$fd}-{$reactorId} Connect.\n\";\n        });\n    }\n\n    public function onReceive($serv, $fd, $reactorId, $data) {\n        go(function() use($serv, $fd, $reactorId, $data) {\n            $message = MyProtocol::decode($data);\n            if($message) {\n                \/\/ Handle message &amp;amp; reply to client\n                $serv-&amp;gt;send($fd, MyProtocol::encode(array('status' =&amp;gt; 0, 'message' =&amp;gt; 'OK')));\n            } else {\n                \/\/ Invalid message, close connection\n                $serv-&amp;gt;close($fd);\n            }\n        });\n    }\n\n    public function onClose($serv, $fd, $reactorId) {\n        go(function() use($fd, $reactorId) {\n            echo \"Client: {$fd}-{$reactorId} Close.\n\";\n        });\n    }\n}\n\nnew Server();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f7f\u7528go(function())\u5c06\u4efb\u52a1\u52a0\u5230\u534f\u7a0b\u4e2d\u6267\u884c\uff0c\u51cf\u5c11\u4e86\u4ee3\u7801\u91cf\uff0c\u540c\u65f6\u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u56de\u8c03\u51fd\u6570\u548c\u624b\u52a8\u7ba1\u7406\u63a7\u5236\u6d41\u7a0b\u7684\u7e41\u7410\u64cd\u4f5c\u3002<\/p>\n<p>\u56db\u3001\u5982\u4f55\u90e8\u7f72<\/p>\n<p>\u901a\u8fc7Swoole\u63d0\u4f9b\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u7ba1\u7406\u8fd0\u884c\u670d\u52a1\u5668\u7684\u8fdb\u7a0b\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u542f\u52a8\u4e00\u4e2aSwoole TCP\u670d\u52a1\u5668\u7684\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre>php server.php<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679c\u9700\u8981\u4fdd\u6301\u670d\u52a1\u5668\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u53ef\u4ee5\u8bbe\u7f6edaemonize\u9009\u9879\uff1a<\/p>\n<pre>php server.php --daemonize<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f7f\u7528Swoole\u63d0\u4f9b\u7684\u547d\u4ee4\u884c\u5de5\u5177\u5f00\u542f\u3001\u91cd\u542f\u548c\u505c\u6b62\u670d\u52a1\u5668\u7b49\u64cd\u4f5c\uff1a<\/p>\n<pre>swoole_server [start|stop|reload|restart|shutdown]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4f7f\u7528Swoole\uff0c\u6211\u4eec\u80fd\u591f\u65b9\u4fbf\u5730\u5b9e\u73b0\u9ad8\u6548\u7684\u5e76\u53d1\u7f51\u7edc\u5e94\u7528\u3002\u4f7f\u7528\u534f\u7a0b\u7f16\u5199\u7684Swoole TCP\u670d\u52a1\u5668\u4e0d\u4ec5\u7b80\u5316\u4e86\u4ee3\u7801\u7ed3\u6784\uff0c\u800c\u4e14\u5177\u6709\u66f4\u9ad8\u7684\u6027\u80fd\uff0c\u80fd\u591f\u4e0e\u4f20\u7edf\u7684\u591a\u8fdb\u7a0b\u6216\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u76f8\u6bd4\u83b7\u5f97\u66f4\u597d\u7684\u5904\u7406\u6027\u80fd\uff0c\u5927\u5e45\u8282\u7701\u4e86\u670d\u52a1\u5668\u7684\u8d44\u6e90\u6d88\u8017\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSwoole\u8fdb\u9636\uff1a\u4f7f\u7528\u534f\u7a0b\u7f16\u5199\u5e76\u53d1\u670d\u52a1\u5668\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>\u5728\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u4f5c\u4e3a\u4e00\u6b3e\u957f\u7a0b\u8fdb\u7a0b\u901a\u4fe1\u6846\u67b6\uff0c\u8d8a\u6765\u8d8a\u53d7\u5230\u5f00\u53d1\u8005\u7684\u9752\u7750\u3002swoole\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7f51\u7edc\u7f16\u7a0bapi\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u5f02\u6b65\u7f16\u7a0b\uff0c\u63d0\u9ad8\u4e86\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528swoole\u548c\u534f\u7a0b\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u5e76\u53d1\u670d\u52a1\u5668\u3002 \u4e00\u3001\u73af\u5883\u642d\u5efa \u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Swoole\u6269\u5c55\uff0c\u5b89\u88c5\u65b9\u6cd5\u53ef\u4ee5\u53c2\u8003Swoole\u5b98\u65b9\u6587\u6863\u3002\u672c\u6587\u4f7f\u7528\u7684\u662fPHP7.2\u7248\u672c\u3002 \u4e8c\u3001\u670d\u52a1\u5668\u7a0b\u5e8f\u6846\u67b6 \u6211\u4eec\u9700\u8981\u4f7f\u7528Swoole\u7684TCP\u670d\u52a1\u5668\uff0c\u5177\u4f53\u5b9e\u73b0\u9700\u8981\u8003\u8651\u5982\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a \u5b9a\u4e49\u534f\u8bae\u683c\u5f0f \u5728\u7f51\u7edc\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u5b9a\u4e49\u4e00\u79cd\u6807\u51c6\u7684\u6570\u636e\u4f20\u8f93\u683c\u5f0f\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u534f\u8bae\u683c\u5f0f\uff0c\u5982\u4e0b\u6240\u793a\uff1a class MyProtocol { const HEADER_SIZE = 4; const MAX_PACKAGE_SIZE = 1024 * 1024; public static function encode($data) { $package = json_encode($data, JSON_UNESCAPED_UNICODE); return pack(&#8216;N&#8217;, strlen($package)) . $package; } public static function decode($buffer) { if(strlen($buffer) self::MAX_PACKAGE_SIZE) { return false; } if(strlen($buffer) &lt;p&gt;\u534f\u8bae\u683c\u5f0f\u5305\u542b\u4e00\u4e2a4\u5b57\u8282\u7684\u5934\u90e8\uff0c\u7528\u4e8e\u5b58\u653e\u6570\u636e\u5305\u7684\u957f\u5ea6\uff0c\u548c\u4e00\u4e2aJSON\u5b57\u7b26\u4e32\u8868\u793a\u7684\u5b9e\u9645\u6570\u636e\u3002\u8fd9\u79cd\u683c\u5f0f\u53ef\u4ee5\u652f\u6301\u4e0d\u540c\u7684\u6d88\u606f\u7c7b\u578b\uff0c\u5e76\u5b9e\u73b0\u4f20\u8f93\u7684\u53ef\u9760\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002&lt;\/p&gt;&lt;ol start=&#8221;2&#8243;&gt;&lt;li&gt;\u5b9a\u4e49\u4e1a\u52a1\u5904\u7406&lt;\/li&gt;&lt;\/ol&gt;&lt;p&gt;Server\u7c7b\u7684\u56de\u8c03\u51fd\u6570\u4e2d\u5b9a\u4e49\u4e1a\u52a1\u903b\u8f91\u5904\u7406\uff0c\u5982\u4e0b\u6240\u793a\uff1a&lt;\/p&gt;&lt;pre class=&#8221;brush:php;toolbar:false;&#8221;&gt;class Server { private $serv; public [&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-23564","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23564","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=23564"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23564\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}