{"id":7610,"date":"2020-11-19T14:10:22","date_gmt":"2020-11-19T06:10:22","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=7610"},"modified":"2022-12-06T13:40:48","modified_gmt":"2022-12-06T05:40:48","slug":"mqtt-websocket","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=7610","title":{"rendered":"MQTT - 3. WebSocket"},"content":{"rendered":"<p>--<\/p>\n<h2>MQTT client\u00a0library<\/h2>\n<ul>\n<li><a href=\"https:\/\/github.com\/mqttjs\/MQTT.js\" target=\"_blank\" rel=\"noopener\" data-pjax=\"#js-repo-pjax-container\">MQTT.js<\/a><\/li>\n<li><a href=\"https:\/\/www.eclipse.org\/paho\/index.php?page=clients\/js\/index.php\" target=\"_blank\" rel=\"noopener\">Eclipse Paho JavaScript Client<\/a><\/li>\n<\/ul>\n<p>\u63a8\u85a6\u4f7f\u7528 MQTT.js \u539f\u56e0\u5982\u4e0b<\/p>\n<ul>\n<li><a href=\"https:\/\/www.emqx.com\/zh\/blog\/connect-to-mqtt-broker-with-websocket\" target=\"_blank\" rel=\"noopener\">\u4f7f\u7528 WebSocket \u8fde\u63a5 MQTT \u670d\u52a1\u5668<\/a><\/li>\n<\/ul>\n<p>--<\/p>\n<h2>MQTT.js<\/h2>\n<ul>\n<li><a href=\"https:\/\/blog.csdn.net\/lcr_happy\/article\/details\/86588770\" target=\"_blank\" rel=\"noopener\">js\u4f7f\u7528mqtt\u7684\u793a\u4f8b\u4ee3\u7801<\/a><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"UTF-8\"&gt;\r\n    &lt;title&gt;MQTT - WebSocket&lt;\/title&gt;\r\n    &lt;script src=\"mqtt.min.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;script&gt;\r\n    var options = {\r\n        \/\/mqtt\u5ba2\u6237\u7aef\u7684id\uff0c\u8fd9\u91cc\u9762\u5e94\u8be5\u8fd8\u53ef\u4ee5\u52a0\u4e0a\u5176\u4ed6\u53c2\u6570\uff0c\u5177\u4f53\u770b\u5b98\u65b9\u6587\u6863\r\n        clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8),\r\n        username: 'hoyo',\r\n        password: 'hoyo'\r\n    };\r\n\r\n    \/\/\u6d4f\u89c8\u5668\u91c7\u7528websocket\u534f\u8bae\uff0chost\u4e3b\u673a\u5730\u5740\u4e3a192.168.0.200\uff0c\u7aef\u53e3\u4e3a9001\uff0c\u8def\u5f84\u4e3a\/mqtt\r\n    var client = mqtt.connect( \"ws:\/\/broker.emqx.io:8083\/mqtt\", options ); \/\/ you add a ws:\/\/ url here\r\n\r\n    \/\/\u5efa\u7acb\u8fde\u63a5\r\n    client.on('connect', function () {\r\n\r\n        \/\/\u8ba2\u9605\u4e3b\u9898 presence\r\n        client.subscribe('presence', function (err) {\r\n            if (!err) {\r\n                console.log(\"subscribe success!\");\r\n                \/\/\u53d1\u5e03\u4e3b\u9898presence,\u6d88\u606f\u5185\u5bb9\u4e3aHello mqtt\r\n                client.publish('presence', 'Hello mqtt')\r\n            }else{\r\n                \/\/\u6253\u5370\u9519\u8bef\r\n                console.log(err)\r\n            }\r\n        })\r\n    });\r\n\r\n    \/\/\u5982\u679c\u8fde\u63a5\u9519\u8bef\uff0c\u6253\u5370\u9519\u8bef\r\n    client.on('error', function (err) {\r\n        console.log(err);\r\n        client.end()\r\n    });\r\n\r\n    \/\/\u5982\u679cclient\u8ba2\u9605\u4e3b\u9898\u6210\u529f\uff0c\u90a3\u4e48\u8fd9\u91cc\u5c31\u662f\u5f53\u63a5\u6536\u5230\u81ea\u5df1\u8ba2\u9605\u4e3b\u9898\u7684\u5904\u7406\u903b\u8f91\r\n    client.on('message', function (topic, message) {\r\n\r\n        \/\/ message is Buffer,\u6b64\u5904\u5c31\u662f\u6253\u5370\u6d88\u606f\u7684\u5177\u4f53\u5185\u5bb9\r\n        console.log(topic, message.toString())\r\n    })\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>--<\/p>\n<h2>\u4f7f\u7528\u516c\u5171 MQTT Borker \u642d\u5efa\u804a\u5929\u5ba4 -\u00a0Eclipse Paho JavaScript Client<\/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<\/a><\/li>\n<li><a href=\"http:\/\/rocksaying.tw\/archives\/2016\/MQTT-2-JavaScript-setting.html\" target=\"_blank\" rel=\"noopener\">MQTT\u7528\u6236\u7aef\u5165\u9580 - \u4e8c\u3001JavaScript \u7528\u6236\u7aef\u7a0b\u5f0f\u8a2d\u8a08<\/a><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"UTF-8\"&gt;\r\n    &lt;title&gt;MQTT - WebSocket&lt;\/title&gt;\r\n    &lt;link rel=\"stylesheet\" href=\"\/3rdParty\/bootstrap-4.3.1\/css\/bootstrap.min.css\"&gt;\r\n    &lt;script src=\"mqttws31.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;script src=\"\/3rdParty\/jquery-1.11.3.min.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n&lt;div id=\"chart_all\"&gt;&lt;\/div&gt;\r\n\r\n&lt;label style=\"position: fixed; bottom: 0;\"&gt;\u7559\u8a00\uff1a&lt;input type=\"text\" id=\"send\" class=\"form-control\" onkeyup=\"send()\"&gt;&lt;\/label&gt;\r\n\r\n&lt;script&gt;\r\n    function $_GET(name) {\r\n        name = name.replace(\/[\\[]\/, \"\\\\[\").replace(\/[\\]]\/, \"\\\\]\");\r\n        var regex = new RegExp(\"[\\\\?&amp;]\" + name + \"=([^&amp;#]*)\"),\r\n            results = regex.exec(location.search);\r\n        return results === null ? \"\" : decodeURIComponent(results[1].replace(\/\\+\/g, \" \"));\r\n    }\r\n    function time() {\r\n        return Math.floor(new Date()\r\n                .getTime() \/ 1000);\r\n    }\r\n\r\n    function send(){\r\n        if (event.keyCode === 13) {\r\n            event.preventDefault();\r\n            event.stopPropagation();\r\n\r\n            message = new Paho.MQTT.Message('{\"time\":\"'+ time() +'\", \"user\":\"'+ user +'\", \"talk\":\"'+$('#send').val()+'\"}');\r\n            message.destinationName = \"\/HoyoMQTTChar\/All\";\r\n            client.send(message);\r\n\r\n            $('#send').val('');\r\n        }\r\n    }\r\n\r\n    var user = $_GET('user');\r\n\r\n    client = new Paho.MQTT.Client('broker.emqx.io', Number(8083), user);\r\n\r\n    \/\/ set callback handlers\r\n    client.onConnectionLost = onConnectionLost;\r\n    client.onMessageArrived = onMessageArrived;\r\n\r\n    \/\/ connect the client\r\n    client.connect({\r\n        onSuccess:onConnect,\r\n        userName : \"hoyo\",\r\n        password : \"hoyo\"\r\n    });\r\n\r\n    \/\/ called when the client connects\r\n    function onConnect() {\r\n        \/\/ Once a connection has been made, make a subscription and send a message.\r\n        console.log(\"onConnect\");\r\n        client.subscribe(\"\/HoyoMQTTChar\/All\");\r\n    }\r\n\r\n    \/\/ called when the client loses its connection\r\n    function onConnectionLost(r) {\r\n        if (r.errorCode !== 0) {\r\n            console.log(\"onConnectionLost:\"+r.errorMessage);\r\n        }\r\n    }\r\n\r\n    \/\/ called when a message arrives\r\n    function onMessageArrived(m) {\r\n        if ( m.destinationName ==='\/HoyoMQTTChar\/All' ){\r\n            var j = JSON.parse(m.payloadString);\r\n            $('#chart_all').append('&lt;div&gt;'+ j.time +' &lt;b&gt;'+ j.user +'&lt;\/b&gt; : '+ j.talk +'&lt;\/div&gt;');\r\n        }\r\n    }\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>\u7d50\u679c<\/p>\n<p><a href=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/11\/20201119_121615.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-7646\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/11\/20201119_121615-300x133.png\" alt=\"\" width=\"300\" height=\"133\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/11\/20201119_121615-300x133.png 300w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/11\/20201119_121615-768x340.png 768w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/11\/20201119_121615-500x221.png 500w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/11\/20201119_121615.png 901w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>--<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<ul>\n<li>WebSocket \u56e0\u70ba\u904b\u4f5c\u5728\u700f\u89bd\u5668\u4e0a\uff0c\u56e0\u6b64\u4e0d\u80fd\u5c07\u96b1\u79c1\u6216\u662f\u7cfb\u7d71\u5b89\u5168\u6d29\u6f0f\uff0c\u4f8b\u5982\u4f7f\u7528\u8005\u5e33\u865f\u3001\u5bc6\u78bc\u3002\u4e5f\u56e0\u70ba\u53ef\u4ee5\u81ea\u8a02\u8a02\u95b1\u4e3b\u984c\u7684\u7279\u6027\uff0c\u70ba\u4e86\u907f\u514d\u906d\u5230\u4e0d\u7576\u4f7f\u7528\u4e5f\u8981\u4f7f\u7528 ACL \u904e\u6ffe\u53ef\u4f9b\u4f7f\u7528\u4e3b\u984c<\/li>\n<li>\u5728\u5b8c\u5168\u4e0d\u5beb Borker \u7a0b\u5f0f\u4ee3\u78bc\u7684\u60c5\u6cc1\u4e0b\uff0c\u57fa\u672c\u529f\u80fd\u90fd\u7121\u6cd5\u5be6\u73fe\uff0c\u4f8b\u5982\u4f7f\u7528\u8005\u6e05\u55ae\uff0c\u56e0\u70ba\u5f8c\u4e00\u4f4d\u4f7f\u7528\u8005\u52a0\u5165\u7121\u6cd5\u53d6\u5f97\u66f4\u65e9\u7684\u4f7f\u7528\u8005\uff0c\u4e5f\u5c31\u662f\u300c\u820a\u8cc7\u6599\u300d\u7684\u63d0\u4f9b\u53ea\u80fd\u5f9e Broker Server \u7aef\u6536\u96c6\u63d0\u4f9b<\/li>\n<\/ul>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"7610\" 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;1,860&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- MQTT client\u00a0...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"7610\" 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;1,860&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":[319],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7610"}],"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=7610"}],"version-history":[{"count":16,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7610\/revisions"}],"predecessor-version":[{"id":11181,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7610\/revisions\/11181"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}