Guest User

Untitled

a guest
Feb 7th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="es">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Dashboard MQTT</title>
  6. <link rel="shortcut icon" type="image/x-icon" href="Imagenes/iot.png">
  7. <script src='https://api.cloudmqtt.com/js/mqttws31.js' type='text/javascript'>
  8. </script>
  9. <!-- https://api.cloudmqtt.com/sso/js/mqttws31.js -->
  10. </head>
  11.  
  12. <body>
  13.  
  14. <header>
  15.  
  16. </header>
  17. <br/>
  18. <h1>WEBSOCKET</h1><br />
  19.  
  20. <div class="table"><form action="" method="post">
  21. <h2>Ingrese los datos</h2>
  22. <label for="host">Host:</label>
  23. <input type="text" id="host" name="host"><br/>
  24. <label for="puerto">Puerto:</label>
  25. <input type="text" id="puerto" name="puerto"><br/>
  26. <input type="submit" onclick="leerdatosformulario()" value="ENVIAR">
  27. </form>
  28. </div>
  29. <script>
  30.  
  31. function leerdatosformulario(){
  32. ls = document.getElementById("host").value;
  33. lss = document.getElementById("puerto").value;
  34. }
  35.  
  36. usuario = 'Prueba';
  37. contrasena = '123';
  38. puerto = 33848;
  39. host = 'm12.cloudmqtt.com';
  40.  
  41. function onConnect() {
  42. // Once a connection has been made, make a subscription and send a message.
  43. console.log("Conexion realizada con el servidor");
  44. client.subscribe("#");
  45. console.log("Conectado como " + usuario + " a través del puerto 8083");
  46. }
  47.  
  48. // called when the client loses its connection
  49. function onConnectionLost(responseObject) {
  50. if (responseObject.errorCode !== 0) {
  51. console.log("onConnectionLost:", responseObject.errorMessage);
  52. setTimeout(function() { client.connect() }, 5000);
  53. }
  54. }
  55.  
  56. // called when a message arrives
  57. function onMessageArrived(message) {
  58.  
  59. var topico = document.getElementsByName("topico-pub")[0].value;
  60. if (message.destinationName == '/' + usuario + '/' + topico) { //acá coloco el topic
  61. document.getElementById("mensaje-pub").textContent = message.payloadString;
  62. console.log("Mensaje recibido:"+ '/' + usuario + '/' + topico + " : " + message.payloadString);
  63. }
  64. }
  65.  
  66. function onFailure(invocationContext, errorCode, errorMessage) {
  67. var errDiv = document.getElementById("error");
  68. errDiv.textContent = "Could not connect to WebSocket server, most likely you're behind a firewall that doesn't allow outgoing connections to port 33848";
  69. errDiv.style.display = "block";
  70. }
  71.  
  72. var clientId = "ws" + Math.random();
  73. // Create a client instance
  74. var client = new Paho.MQTT.Client(host, puerto, clientId);
  75.  
  76. // set callback handlers
  77. client.onConnectionLost = onConnectionLost;
  78. client.onMessageArrived = onMessageArrived;
  79.  
  80. // connect the client
  81. client.connect({
  82. useSSL: true,
  83. userName: usuario,
  84. password: contrasena,
  85. onSuccess: onConnect,
  86. onFailure: onFailure
  87. });
  88. </script>
  89.  
  90. </body>
  91. </html>
Add Comment
Please, Sign In to add comment