MQTT_Demo.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <!-- <body style="margin: 0 auto;">
  2. <div style="width: 500px;">服务器地址:<input style="float: right;width: 300px;" id="MQTT_ServerAdd"
  3. value="ws://www.passingworld.vip:8083/mqtt"></div>
  4. <div style="width: 500px;">心跳时间 :<input style="float: right;width: 300px;"
  5. id="MQTT_ConnHeartbeatTime" value="30"></div>
  6. <div style="width: 500px;">用户名 :<input style="float: right;width: 300px;" id="MQTT_UserName"
  7. value="admin"></div>
  8. <div style="width: 500px;">密码 :<input style="float: right;width: 300px;" id="MQTT_PassWord"
  9. value="public"></div>
  10. <div>
  11. <button onclick="MQTT_Connect()">连接服务器</button>
  12. </div>
  13. <div style="margin-top: 20px;">
  14. 订阅消息<input id="MQTT_Subscribe_Msg">
  15. QoS<select id="MQTT_Subscribe_QoS">
  16. <option>0_Almost_Once</option>
  17. <option>1_Atleast_Once</option>
  18. <option>2_Exactly_Once</option>
  19. </select>
  20. <button onclick="Add_MQTT_Subscribe()">添加订阅信息</button>
  21. </div>
  22. <div style="margin-top: 20px;">
  23. 订阅列表
  24. </div>
  25. <div>
  26. <table style="padding: 1px;width: 700px;margin-top: 5px;" border="1">
  27. <tr style="background-color: #919191;">
  28. <td>订阅消息</td>
  29. <td>QoS</td>
  30. <td>操作</td>
  31. </tr>
  32. </table>
  33. </div>
  34. <div style="margin-top: 20px;">
  35. 接收消息
  36. </div>
  37. <div>
  38. <textarea id="textareaid" rows="5" cols="30" class="message1"
  39. style="width: 100%;height: 300px; min-height: 100px;"></textarea>
  40. </div>
  41. <div>
  42. 发布点:<input id="MQTT_Publish_Text">
  43. 发布消息:<input id="MQTT_Publish_Msg">
  44. <button onclick="Send_MQTT_Msg()">发送消息</button>
  45. </div>
  46. </body>
  47. <script>
  48. function MQTT_Connect(serveradd, KeepAlive, UserName, PassWord) {
  49. //MQTT连接的配置
  50. MQTT_Options: {
  51. protocolVersion: 4; //MQTT连接协议版本
  52. clientId: 'miniTest22222';
  53. clean: false;
  54. keepalive: KeepAlive;
  55. password: UserName;
  56. username: PassWord;
  57. reconnectPeriod: 1000; //1000毫秒,两次重新连接之间的间隔
  58. connectTimeout: 10 * 1000; //1000毫秒,两次重新连接之间的间隔
  59. resubscribe: true; //如果连接断开并重新连接,则会再次自动订阅已订阅的主题(默认true)
  60. };
  61. //开始连接
  62. MQTT_Client = mqtt.connect(serveradd, this.MQTT_Options);
  63. MQTT_Client.on('connect',
  64. function (connack) {
  65. console.log('MQTT连接成功')
  66. })
  67. //服务器下发消息的回调
  68. MQTT_Client.on("message", function (topic, payload) {
  69. console.log(" 收到 topic:" + topic + " , payload :" + payload)
  70. var str = document.getElementById("textareaid").value;
  71. document.getElementById("textareaid").value = str + "\n" + "收到 topic:" + topic + " , payload :" + payload;
  72. })
  73. //服务器连接异常的回调
  74. MQTT_Client.on("error", function (error) { console.log("MQTT Server Error 的回调" + error) })
  75. //服务器重连连接异常的回调
  76. MQTT_Client.on("reconnect", function () { console.log("MQTT Server Reconnect的回调") })
  77. //服务器连接异常的回调
  78. MQTT_Client.on("offline", function (errr) { console.log("MQTT Server Offline的回调" + errr) })
  79. };
  80. MQTT_SubOne = function (Topic) {
  81. var ok = false;
  82. if (MQTT_Client && MQTT_Client.connected) {
  83. MQTT_Client.subscribe(Topic, function (err, granted) {
  84. if (!err) {
  85. console.log('订阅主题 ' + Topic + ' 成功')
  86. ok = true;
  87. } else {
  88. console.log('订阅主题 ' + Topic + ' 失败')
  89. ok = false;
  90. }
  91. })
  92. } else {
  93. console.log('请先连接服务器')
  94. ok = false;
  95. }
  96. return ok;
  97. };
  98. function MQTT_SubMany() {
  99. if (MQTT_Client && MQTT_Client.connected) {
  100. MQTT_Client.subscribe({ 'Topic1': { qos: 0 }, 'Topic2': { qos: 1 } }, function (err, granted) {
  101. if (!err) {
  102. console.log('订阅多主题成功')
  103. } else {
  104. console.log('订阅多主题失败')
  105. }
  106. })
  107. } else {
  108. console.log('请先连接服务器')
  109. }
  110. };
  111. MQTT_PubMsg = function (Topic, Msg) {
  112. if (MQTT_Client && MQTT_Client.connected) {
  113. MQTT_Client.publish(Topic, Msg);
  114. console.log('发布成功->' + Topic + '->' + Msg)
  115. } else {
  116. console.log('请先连接服务器')
  117. }
  118. };
  119. function MQTT_UnSubOne(Topic) {
  120. if (MQTT_Client && MQTT_Client.connected) {
  121. MQTT_Client.unsubscribe(Topic);
  122. } else {
  123. console.log('请先连接服务器')
  124. }
  125. };
  126. function MQTT_unSubMany() {
  127. if (MQTT_Client && MQTT_Client.connected) {
  128. MQTT_Client.unsubscribe(['Topic1', 'Topic2']);
  129. } else {
  130. console.log('请先连接服务器')
  131. }
  132. };
  133. </script> -->
  134. <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
  135. <script>
  136. const mqtt = require('mqtt')
  137. // import mqtt from 'mqtt'
  138. // 连接选项
  139. const options = {
  140. clean: true, // true: 清除会话, false: 保留会话
  141. connectTimeout: 4000, // 超时时间
  142. // 认证信息
  143. clientId: 'emqx_test_html',
  144. // username: 'emqx_test',
  145. // password: 'emqx_test',
  146. }
  147. const connectUrl = 'ws://emqx@127.0.0.1:8084/mqtt'
  148. const client = mqtt.connect(connectUrl, options)
  149. client.on('reconnect', (error) => {
  150. console.log('正在重连:', error)
  151. })
  152. client.on('error', (error) => {
  153. console.log('连接失败:', error)
  154. })
  155. client.on('message', (topic, message) => {
  156. console.log('收到消息:', topic, message.toString())
  157. })
  158. </script>