Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

41 rader
1.2 KiB

  1. <html>
  2. <head>
  3. <title>paho JS Example</title>
  4. <script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript"></script>
  5. <script type="text/javascript">
  6. var mqttClient = new Paho.MQTT.Client("localhost", 80, "/mqtt", "pahoJS_client");
  7. mqttClient.onConnectionLost = onConnectionLost;
  8. mqttClient.onMessageArrived = onMessageArrived;
  9. mqttClient.connect({ onSuccess: onConnect });
  10. function onConnect() {
  11. console.log("MQTT client connected");
  12. mqttClient.subscribe("pahoJS_subscription");
  13. }
  14. function onConnectionLost(responseObject) {
  15. if (responseObject.errorCode !== 0) {
  16. console.log("onConnectionLost:" + responseObject.errorMessage);
  17. }
  18. }
  19. function onMessageArrived(message) {
  20. console.log("onMessageArrived:" + message.payloadString);
  21. }
  22. function buttonClick() {
  23. message = new Paho.MQTT.Message("Hello");
  24. message.destinationName = "pahoJS_topic";
  25. mqttClient.send(message);
  26. }
  27. </script>
  28. </head>
  29. <body>
  30. <button onclick="buttonClick()">Publish</button>
  31. </body>
  32. </html>