{"id":143,"date":"2023-03-24T15:42:01","date_gmt":"2023-03-24T07:42:01","guid":{"rendered":"https:\/\/mianka.xyz\/?p=142"},"modified":"2023-03-24T15:43:09","modified_gmt":"2023-03-24T07:43:09","slug":"swoolemqtt","status":"publish","type":"post","link":"https:\/\/www.mianka.xyz\/?p=143","title":{"rendered":"\u7528swoole\u5199\u4e00\u4e2aMQTT\u670d\u52a1\u5668\u4ee3\u7801"},"content":{"rendered":"<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f7f\u7528Swoole\u7f16\u5199\u7684MQTT\u670d\u52a1\u5668\u4ee3\u7801\u793a\u4f8b\uff0c\u5b83\u53ef\u4ee5\u76d1\u542c\u6307\u5b9a\u7aef\u53e3\u7684\u8fde\u63a5\uff0c\u5e76\u4e14\u53ef\u4ee5\u63a5\u53d7\u6765\u81ea\u5ba2\u6237\u7aef\u7684MQTT\u6d88\u606f\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-php\">&lt;?php\n\/\/&nbsp;\u521b\u5efa\u4e00\u4e2a\u5f02\u6b65TCP\u670d\u52a1\u5668\n$server&nbsp;=&nbsp;new&nbsp;Swoole\\Server(&#39;0.0.0.0&#39;,&nbsp;1883,&nbsp;SWOOLE_PROCESS,&nbsp;SWOOLE_SOCK_TCP&nbsp;|&nbsp;SWOOLE_SSL);\n\/\/&nbsp;\u8bbe\u7f6e\u670d\u52a1\u5668\u53c2\u6570\n$server-&gt;set([\n&nbsp;&nbsp;&nbsp;&nbsp;&#39;worker_num&#39;&nbsp;=&gt;&nbsp;4,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;worker\u8fdb\u7a0b\u6570\n&nbsp;&nbsp;&nbsp;&nbsp;&#39;ssl_cert_file&#39;&nbsp;=&gt;&nbsp;&#39;\/path\/to\/server.crt&#39;,&nbsp;&nbsp;\/\/&nbsp;SSL\u8bc1\u4e66\u6587\u4ef6\u8def\u5f84\n&nbsp;&nbsp;&nbsp;&nbsp;&#39;ssl_key_file&#39;&nbsp;=&gt;&nbsp;&#39;\/path\/to\/server.key&#39;,&nbsp;&nbsp;\/\/&nbsp;SSL\u79c1\u94a5\u6587\u4ef6\u8def\u5f84\n]);\n\/\/&nbsp;\u76d1\u542c\u8fde\u63a5\u4e8b\u4ef6\n$server-&gt;on(&#39;connect&#39;,&nbsp;function&nbsp;(Swoole\\Server&nbsp;$server,&nbsp;$fd)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;Client&nbsp;#{$fd}:&nbsp;connected.\\n&quot;;\n});\n\/\/&nbsp;\u76d1\u542c\u6570\u636e\u63a5\u6536\u4e8b\u4ef6\n$server-&gt;on(&#39;receive&#39;,&nbsp;function&nbsp;(Swoole\\Server&nbsp;$server,&nbsp;$fd,&nbsp;$from_id,&nbsp;$data)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5904\u7406MQTT\u6d88\u606f\n&nbsp;&nbsp;&nbsp;&nbsp;$mqtt_message&nbsp;=&nbsp;new&nbsp;Swoole\\Coroutine\\MQTT\\Message($data);\n&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;Received&nbsp;message&nbsp;from&nbsp;client&nbsp;#{$fd}:&nbsp;{$mqtt_message-&gt;getPayload()}\\n&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u56de\u590d\u786e\u8ba4\u6d88\u606f\n&nbsp;&nbsp;&nbsp;&nbsp;$mqtt_ack&nbsp;=&nbsp;new&nbsp;Swoole\\Coroutine\\MQTT\\Ack(Swoole\\Coroutine\\MQTT\\Ack::CONNECT,&nbsp;0);\n&nbsp;&nbsp;&nbsp;&nbsp;$server-&gt;send($fd,&nbsp;$mqtt_ack-&gt;encode());\n});\n\/\/&nbsp;\u76d1\u542c\u5173\u95ed\u4e8b\u4ef6\n$server-&gt;on(&#39;close&#39;,&nbsp;function&nbsp;(Swoole\\Server&nbsp;$server,&nbsp;$fd)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;Client&nbsp;#{$fd}:&nbsp;closed.\\n&quot;;\n});\n\/\/&nbsp;\u542f\u52a8\u670d\u52a1\u5668\n$server-&gt;start();<\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Swoole\u7684Swoole\\Server\u7c7b\u6765\u521b\u5efa\u4e00\u4e2aTCP\u670d\u52a1\u5668\uff0c\u5b83\u4f1a\u76d1\u542c\u57281883\u7aef\u53e3\u4e0a\u3002worker_num\u53c2\u6570\u8bbe\u7f6e\u4e86\u670d\u52a1\u5668\u7684worker\u8fdb\u7a0b\u6570\uff0cssl_cert_file\u548cssl_key_file\u53c2\u6570\u7528\u4e8e\u542f\u7528SSL\/TLS\u52a0\u5bc6\u3002<\/p>\n<p><\/p>\n<p>\u6211\u4eec\u5b9a\u4e49\u4e86\u4e09\u4e2a\u56de\u8c03\u51fd\u6570\u6765\u5904\u7406\u8fde\u63a5\u3001\u6570\u636e\u63a5\u6536\u548c\u5173\u95ed\u4e8b\u4ef6\u3002\u5728\u6570\u636e\u63a5\u6536\u4e8b\u4ef6\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Swoole\u7684Swoole\\Coroutine\\MQTT\\Message\u7c7b\u6765\u89e3\u6790MQTT\u6d88\u606f\uff0c\u4f7f\u7528Swoole\\Coroutine\\MQTT\\Ack\u7c7b\u6765\u56de\u590d\u786e\u8ba4\u6d88\u606f\u3002<\/p>\n<p><\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u6b64\u793a\u4f8b\u4ec5\u4f9b\u53c2\u8003\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u6839\u636e\u60a8\u7684\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u66f4\u6539\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5728\u670d\u52a1\u5668\u542f\u52a8\u65f6\u8fdb\u884c\u4e00\u4e9b\u521d\u59cb\u5316\u64cd\u4f5c\uff0c\u6216\u8005\u9700\u8981\u5728\u6570\u636e\u63a5\u6536\u4e8b\u4ef6\u4e2d\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\u3002<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f7f\u7528Swoole\u7f16\u5199\u7684MQTT\u670d\u52a1\u5668\u4ee3\u7801\u793a\u4f8b\uff0c\u5b83\u53ef\u4ee5\u76d1\u542c\u6307\u5b9a\u7aef\u53e3\u7684\u8fde\u63a5\uff0c\u5e76\u4e14\u53ef\u4ee5\u63a5\u53d7\u6765\u81ea\u5ba2\u6237\u7aef\u7684MQTT\u6d88\u606f\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-php\">&lt;?php\n\/\/&nbsp;\u521b\u5efa\u4e00\u4e2a\u5f02\u6b65TCP\u670d\u52a1\u5668\n$server&nbsp;=&nbsp;new&nbsp;Swoole\\Server(&#39;0.0.0.0&#39;,&nbsp;1883,&nbsp;SWOOLE_PROCESS,&nbsp;SWOOLE_SOCK_TCP&nbsp;|&nbsp;SWOOLE_SSL);\n\/\/&nbsp;\u8bbe\u7f6e\u670d\u52a1\u5668\u53c2\u6570\n$server-&gt;set([\n&nbsp;&nbsp;&nbsp;&nbsp;&#39;worker_num&#39;&nbsp;=&gt;&nbsp;4,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;worker\u8fdb\u7a0b\u6570\n&nbsp;&nbsp;&nbsp;&nbsp;&#39;ssl_cert_file&#39;&nbsp;=&gt;&nbsp;&#39;\/path\/to\/server.crt&#39;,&nbsp;&nbsp;\/\/&nbsp;SSL\u8bc1\u4e66\u6587\u4ef6\u8def\u5f84\n&nbsp;&nbsp;&nbsp;&nbsp;&#39;ssl_key_file&#39;&nbsp;=&gt;&nbsp;&#39;\/path\/to\/server.key&#39;,&nbsp;&nbsp;\/\/&nbsp;SSL\u79c1\u94a5\u6587\u4ef6\u8def\u5f84\n]);\n\/\/&nbsp;\u76d1\u542c\u8fde\u63a5\u4e8b\u4ef6\n$server-&gt;on(&#39;connect&#39;,&nbsp;function&nbsp;(Swoole\\Server&nbsp;$server,&nbsp;$fd)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;Client&nbsp;#{$fd}:&nbsp;connected.\\n&quot;;\n});\n\/\/&nbsp;\u76d1\u542c\u6570\u636e\u63a5\u6536\u4e8b\u4ef6\n$server-&gt;on(&#39;receive&#39;,&nbsp;function&nbsp;(Swoole\\Server&nbsp;$server,&nbsp;$fd,&nbsp;$from_id,&nbsp;$data)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5904\u7406MQTT\u6d88\u606f\n&nbsp;&nbsp;&nbsp;&nbsp;$mqtt_message&nbsp;=&nbsp;new&nbsp;Swoole\\Coroutine\\MQTT\\Message($data);\n&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;Received&nbsp;message&nbsp;from&nbsp;client&nbsp;#{$fd}:&nbsp;{$mqtt_message-&gt;getPayload()}\\n&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u56de\u590d\u786e\u8ba4\u6d88\u606f\n&nbsp;&nbsp;&nbsp;&nbsp;$mqtt_ack&nbsp;=&nbsp;new&nbsp;Swoole\\Coroutine\\MQTT\\Ack(Swoole\\Coroutine\\MQTT\\Ack::CONNECT,&nbsp;0);\n&nbsp;&nbsp;&nbsp;&nbsp;$server-&gt;send($fd,&nbsp;$mqtt_ack-&gt;encode());\n});\n\/\/&nbsp;\u76d1\u542c\u5173\u95ed\u4e8b\u4ef6\n$server-&gt;on(&#39;close&#39;,&nbsp;function&nbsp;(Swoole\\Server&nbsp;$server,&nbsp;$fd)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;Client&nbsp;#{$fd}:&nbsp;closed.\\n&quot;;\n});\n\/\/&nbsp;\u542f\u52a8\u670d\u52a1\u5668\n$server-&gt;start();<\/pre>\n<p><!--autointro--><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-143","post","type-post","status-publish","format-standard","hentry","category-phpbiji"],"_links":{"self":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=143"}],"version-history":[{"count":0,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/143\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}