callMqttViaWebSocket.html 759 B

123456789101112131415161718192021222324
  1. <html>
  2. <head>
  3. <title>test Ws mqtt.js</title>
  4. </head>
  5. <body>
  6. <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
  7. <script>
  8. var client = mqtt.connect('ws://localhost:8384/mqtt') // you add a ws:// url here
  9. client.on('connect', function () {
  10. client.subscribe('/sys/fdcServerApp/Applications.FDC.FdcServerHostApp/thing/event/OnFdcControllerStateChange/post', function (err) {
  11. if (!err) {
  12. client.publish('presence', 'Hello mqtt')
  13. }
  14. })
  15. })
  16. client.on('message', function (topic, message) {
  17. // message is Buffer
  18. console.log(message.toString())
  19. client.end()
  20. })
  21. </script>
  22. </body>
  23. </html>