{"id":13345,"date":"2024-07-09T11:31:01","date_gmt":"2024-07-09T03:31:01","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=13345"},"modified":"2024-08-28T09:12:26","modified_gmt":"2024-08-28T01:12:26","slug":"mqtt-%e4%bd%bf%e7%94%a8-paho-mqtt-javascript","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=13345","title":{"rendered":"MQTT - \u4f7f\u7528 Paho MQTT JavaScript"},"content":{"rendered":"<p>--<\/p>\n<h2>\u524d\u8a00<\/h2>\n<p>\u4e4b\u524d\u4e00\u76f4\u4f7f\u7528 MQTT.js\uff0c\u5f8c\u4f86\u51fa\u73fe\u4e86\u300c<a href=\"https:\/\/github.com\/mqttjs\/MQTT.js\/issues\/1361\" target=\"_blank\" rel=\"noopener\">WebSocket connection to 'ws:\/\/xxx\/mqtt' failed: Close received after close<\/a>\u300d\u932f\u8aa4\uff0c\u96d6\u7136\u6703\u81ea\u52d5\u91cd\u9023\uff0c\u4e0d\u904e\u5c31\u8981\u8655\u7406\u91cd\u9023\u521d\u59cb\u5316\u7684\u8a0a\u606f\u3002\u53ef\u4ee5\u8655\u7406\u4e0d\u904e\u5728 google \u8ffd\u7a76\u4e4b\u5f8c\uff0c\u5c31\u6c7a\u5b9a\u6539\u7528 Paho MQTT \u8a66\u8a66\u770b\uff0c\u7562\u7adf\u5728 Python \u4e5f\u662f\u4f7f\u7528 Paho MQTT \u7684\u7248\u672c\uff0c\u8aaa\u4e0d\u5b9a\u53ef\u4ee5\u89e3\u6c7a\u554f\u984c<\/p>\n<p>--<\/p>\n<h2>\u53c3\u8003\u8cc7\u6e90<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.hivemq.com\/blog\/mqtt-client-library-encyclopedia-paho-js\/\" target=\"_blank\" rel=\"noopener\">Paho JavaScript - MQTT Client Library Encyclopedia (hivemq.com)<\/a><\/li>\n<li><a href=\"https:\/\/eclipse.dev\/paho\/index.php?page=downloads.php\" target=\"_blank\" rel=\"noopener\">Eclipse Paho | The Eclipse Foundation<\/a><\/li>\n<\/ul>\n<p>Paho MQTT JavaScript <strong>1.0.3<\/strong> \u7248\u672c\u622a\u81f3 2024\/07 \u53ea\u652f\u63f4 3.1.1\uff0c\u5c0d\u65bc\u7db2\u9801 WebSocket \u800c\u8a00\u4e26\u4e0d\u5f71\u97ff<\/p>\n<p>--<\/p>\n<h2>\u7a0b\u5f0f\u78bc<\/h2>\n<pre class=\"lang:default decode:true\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"zh-TW\"&gt;\r\n&lt;head&gt;\r\n    &lt;meta http-equiv=\"Content-Type\" content=\"text\/html;charset=UTF-8\"&gt;\r\n    &lt;meta charset=\"UTF-8\"&gt;\r\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0\"&gt;\r\n    &lt;title&gt;Paho MQTT JS Test&lt;\/title&gt;\r\n    &lt;link href=\"https:\/\/cdn.jsdelivr.net\/npm\/fastbootstrap@2.2.0\/dist\/css\/fastbootstrap.min.css\" rel=\"stylesheet\"&gt;\r\n    &lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/paho-mqtt@1.0.3\/mqttws31-min.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n&lt;button type=\"button\" class=\"btn btn-light\" onclick=\"send()\"&gt;Send&lt;\/button&gt;\r\n\r\n&lt;script src=\"php.js?v=65cd879be1395\"&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\n    let mc;\r\n    let mqtt_client_id = window.localStorage.client_id + '_' + Math.random().toString(16);\r\n\r\n    mc = new Paho.MQTT.Client(\"broker.hivemq.com\", 8884, mqtt_client_id);\r\n    mc.connect({\r\n        onSuccess: onConnect,\r\n        useSSL: true\r\n    });\r\n\r\n    \/\/ set callback handlers\r\n    mc.onConnectionLost = function (responseObject) {\r\n        console.log(responseObject.errorCode);\r\n        console.log(\"Connection Lost: \"+responseObject.errorMessage);\r\n    }\r\n\r\n    mc.onMessageArrived = function (message) {\r\n        console.log(message.payloadString);\r\n    }\r\n\r\n    \/\/ Called when the connection is made\r\n    function onConnect(){\r\n        console.log(\"Connected!\");\r\n        mc.subscribe(\"HootKaSelf\/lobby\", {\r\n            onSuccess: function (){\r\n                console.log('subscribeSuccess');\r\n            },\r\n            onFailure: function (){}\r\n        });\r\n\r\n        let message = new Paho.MQTT.Message(\"Hello\");\r\n        message.destinationName = \"HootKaSelf\/lobby\";\r\n        mc.send(message);\r\n    }\r\n\r\n    function send(){\r\n        let message = new Paho.MQTT.Message(JSON.stringify({\r\n            command: \"question\",\r\n            room_id: window.localStorage.in_room,\r\n            order: '\u5927\u5bb6\u597d',\r\n        }));\r\n        console.log(message);\r\n        message.destinationName = \"HootKaSelf\/lobby\";\r\n        mc.send(message);\r\n    }\r\n&lt;\/script&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>--<\/p>\n<h2>\u5b89\u88dd\u3001\u9023\u63a5<\/h2>\n<p>\u5b89\u88dd\u4f7f\u7528 CDN\uff0cMQTT Broker \u4f7f\u7528\u4e86 HiveMQ \u7684<\/p>\n<pre class=\"lang:js decode:true\">&lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/paho-mqtt@1.0.3\/mqttws31-min.js\"&gt;&lt;\/script&gt;\r\n\r\n&lt;script&gt;\r\nmc = new Paho.MQTT.Client('broker.hivemq.com', 8884, 'mqtt_client_id');\r\nmc.connect({\r\n    onSuccess: onConnect,\r\n    useSSL: true,\r\n});\r\n\r\nfunction onConnect(){\r\n    \/\/ \u9023\u63a5\u6210\u529f\u8a02\u95b1\u4e3b\u984c\r\n    mc.subscribe(\"HootKaSelf\/lobby\", {\r\n        onSuccess: function (){\r\n            console.log('subscribeSuccess');\r\n        },\r\n        onFailure: function (){}\r\n    });\r\n}\r\n&lt;\/script&gt;<\/pre>\n<p>.connect() \u9078\u9805<\/p>\n<ul>\n<li>timeout:\"number\"<\/li>\n<li>userName:\"string\"<\/li>\n<li>password:\"string\"<\/li>\n<li>willMessage:\"object\"<\/li>\n<li>keepAliveInterval:\"number\"<\/li>\n<li>cleanSession:\"boolean\"<\/li>\n<li>useSSL:\"boolean\"<\/li>\n<li>invocationContext:\"object\"<\/li>\n<li>onSuccess:\"function\"<\/li>\n<li>onFailure:\"function\"<\/li>\n<li>hosts:\"object\"<\/li>\n<li>ports:\"object\"<\/li>\n<li>mqttVersion:\"number\"<\/li>\n<li>mqttVersionExplicit:\"boolean\"<\/li>\n<li>uris: \"object\"<\/li>\n<\/ul>\n<p>\u5982\u679c Broker \u9700\u8981\u5e33\u865f\u3001\u5bc6\u78bc\uff0c\u5c31\u9700\u8981\u589e\u52a0 userName password \u9078\u9805<\/p>\n<p>--<\/p>\n<h2>\u8a02\u95b1\u3001\u53d6\u6d88\u8a02\u95b1\u4e3b\u984c<\/h2>\n<p>\u8a02\u95b1\u4e3b\u984c<\/p>\n<pre class=\"lang:default decode:true\">mc.subscribe(\"HootKaSelf\/lobby\", {\r\n    onSuccess: function (){\r\n        console.log('subscribeSuccess');\r\n    },\r\n    onFailure: function (){}\r\n});<\/pre>\n<p>\u53d6\u6d88\u8a02\u95b1\u4e3b\u984c<\/p>\n<pre class=\"lang:default decode:true\">mc.unsubscribe('HootKaSelf\/room\/' + j['room_id'], {\r\n    onFailure: function () {},\r\n    onSuccess: function () {\r\n        console.log(\"subscribe success!\");\r\n    }\r\n});<\/pre>\n<p>--<\/p>\n<h2>\u50b3\u9001\u8a0a\u606f<\/h2>\n<p>\u4f7f\u7528 JSON \u683c\u5f0f<\/p>\n<pre class=\"lang:js decode:true\">let message = new Paho.MQTT.Message(JSON.stringify({\r\n    command: \"start_game\",\r\n    room_id: room_id,\r\n    user_id: client_id,\r\n}));\r\nmessage.destinationName = \"HootKaSelf\/server\";\r\nmc.send(message);<\/pre>\n<p>--<\/p>\n<h2>\u63a5\u6536\u8a0a\u606f<\/h2>\n<pre class=\"lang:js decode:true\">mc.onMessageArrived = function (message) {\r\n    let json = JSON.parse(message.payloadString);\r\n}<\/pre>\n<p>\u63a5\u6536\u7684\u8a0a\u606f\u7d93\u904e\u8655\u7406\u8f49\u63db\u6210 JSON \u683c\u5f0f<\/p>\n<p>messge \u53ef\u63a5\u6536\u5230<\/p>\n<ul>\n<li>destinationName \u4e3b\u984c<\/li>\n<li>duplicate \u662f\u5426\u70ba\u91cd\u8907\u767c\u9001<\/li>\n<li>payloadBytes \u5b57\u6578<\/li>\n<li>payloadString \u5167\u5bb9<\/li>\n<li>qos<\/li>\n<li>retained<\/li>\n<\/ul>\n<p>--<\/p>\n<h2>AMQJS0005E Internal error.<\/h2>\n<p>Paho MQTT \u51fa\u73fe\u9019\u500b\u932f\u8aa4\u7684\u8a71\u662f onMessageArrived \u63a5\u6536\u8a0a\u606f\u8655\u7406\u6642\u7a0b\u5f0f\u6709\u932f\u8aa4\uff0c\u5c0e\u81f4 WebSocket \u65b7\u7dda\uff0c\u9019\u6642 Paho \u6703\u6514\u622a\u932f\u8aa4\u52a0\u4e0a\u81ea\u5df1\u7684\u8cc7\u8a0a\u3002\u6240\u4ee5\u6703\u8b93\u4eba\u8aa4\u6703\u662f MQTT \u9023\u63a5\u554f\u984c\uff0c\u5176\u5be6\u53ea\u662f\u63a5\u53d7\u8a0a\u606f\u5167\u7684\u7a0b\u5f0f\u51fa\u932f\u3002<\/p>\n<p>--<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Paho MQTT \u78ba\u5be6\u89e3\u6c7a\u4e86 MQTT.js \u7684\u554f\u984c\uff0cPaho MQTT \u7684\u5fc3\u8df3\u4fdd\u6301\u529f\u80fd\u6bd4\u8f03\u6b63\u5e38\uff0c\u4e00\u5929\u7684\u6e2c\u8a66\u4e4b\u4e0b\u90fd\u662f\u7dad\u6301\u540c\u4e00\u500b\u9023\u63a5\uff0c\u4e26\u4e0d\u6703\u91cd\u65b0\u9023\u63a5\u3002\u53cd\u4e4b MQTT.js \u5247\u662f\u591a\u6b21\u91cd\u65b0\u9023\u63a5\uff0c\u4e0d\u77e5\u9053\u539f\u56e0\u70ba\u4f55\u3002<\/p>\n<p>\u6700\u5f8c (2024\/07) Hoyo \u63a8\u85a6 JavaScript \u4f7f\u7528 Paho MQTT<\/p>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"13345\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;2,734&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- \u524d\u8a00 \u4e4b\u524d\u4e00\u76f4\u4f7f\u7528 MQ...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"13345\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;2,734&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[272,319],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/13345"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13345"}],"version-history":[{"count":5,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/13345\/revisions"}],"predecessor-version":[{"id":13362,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/13345\/revisions\/13362"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}