| 1 | <!DOCTYPE html> | 
  | 2 | <html> | 
  | 3 |  | 
  | 4 | <head> | 
  | 5 |  | 
  | 6 |  <meta charset="UTF-8"> | 
  | 7 |  <meta name="viewport" content="width=device-width"> | 
  | 8 |  | 
  | 9 |  <title>Chat</title> | 
  | 10 |  | 
  | 11 |  <script src="paho.javascript-1.0.3/paho-mqtt-min.js"></script> | 
  | 12 |  | 
  | 13 | </head> | 
  | 14 |  | 
  | 15 | <body> | 
  | 16 |  | 
  | 17 |  <form onsubmit="formActivada(event)"> | 
  | 18 |  | 
  | 19 |   <h1>Chat</h1> | 
  | 20 |  | 
  | 21 |   <p> | 
  | 22 |    <label> | 
  | 23 |     Alias * | 
  | 24 |     <input id="inputAlias" required> | 
  | 25 |    </label> | 
  | 26 |   </p> | 
  | 27 |  | 
  | 28 |   <p> | 
  | 29 |    <label> | 
  | 30 |     Mensaje * | 
  | 31 |     <input id="inputMensaje" required> | 
  | 32 |    </label> | 
  | 33 |   </p> | 
  | 34 |  | 
  | 35 |   <p>* Obligatorio</p> | 
  | 36 |  | 
  | 37 |   <p><button type="submit">Enviar</button></p> | 
  | 38 |  | 
  | 39 |   <pre id="pre"></pre> | 
  | 40 |  | 
  | 41 |  </form> | 
  | 42 |  | 
  | 43 |  <script type="module"> | 
  | 44 |  | 
  | 45 |   import { exportaAHtml } from "./lib/js/exportaAHtml.js" | 
  | 46 |   import { creaIdCliente } from "./lib/js/creaIdCliente.js" | 
  | 47 |   import { falloEnLaConexionMqtt } from "./lib/js/falloEnLaConexionMqtt.js" | 
  | 48 |   import { conexionMqttPerdida } from "./lib/js/conexionMqttPerdida.js" | 
  | 49 |   import { muestraError } from "./lib/js/muestraError.js" | 
  | 50 |  | 
  | 51 |   const TOPICO_CHAT = "gilpgawoas/chat" | 
  | 52 |  | 
  | 53 |    | 
  | 54 |   const clientId = creaIdCliente("gilpgawoasChat-") | 
  | 55 |  | 
  | 56 |    | 
  | 57 |   const cliente = new Paho.MQTT.Client("test.mosquitto.org", 8081, clientId) | 
  | 58 |  | 
  | 59 |    | 
  | 60 |  | 
  | 61 |  | 
  | 62 |   function formActivada(event) { | 
  | 63 |    try { | 
  | 64 |     event.preventDefault() | 
  | 65 |     const mensaje = `${inputAlias.value.trim()} | 
  | 66 | ${inputMensaje.value.trim()}` | 
  | 67 |     enviaMensajeMqtt(mensaje, TOPICO_CHAT) | 
  | 68 |    } catch (error) { | 
  | 69 |     muestraError(error) | 
  | 70 |    } | 
  | 71 |   } | 
  | 72 |   exportaAHtml(formActivada) | 
  | 73 |  | 
  | 74 |    | 
  | 75 |   cliente.onMessageArrived = mensaje => { | 
  | 76 |    if (mensaje.destinationName === TOPICO_CHAT) { | 
  | 77 |     pre.textContent += mensaje.payloadString + "\n\n" | 
  | 78 |    } | 
  | 79 |   } | 
  | 80 |  | 
  | 81 |    | 
  | 82 |   cliente.onConnectionLost = conexionMqttPerdida | 
  | 83 |  | 
  | 84 |    | 
  | 85 |   cliente.connect({ | 
  | 86 |  | 
  | 87 |    keepAliveInterval: 10, | 
  | 88 |  | 
  | 89 |    useSSL: true, | 
  | 90 |  | 
  | 91 |     | 
  | 92 |    onFailure: falloEnLaConexionMqtt, | 
  | 93 |  | 
  | 94 |     | 
  | 95 |    onSuccess: () => { | 
  | 96 |     console.log("Conectado") | 
  | 97 |      | 
  | 98 |     cliente.subscribe(TOPICO_CHAT) | 
  | 99 |    }, | 
  | 100 |  | 
  | 101 |   }) | 
  | 102 |  | 
  | 103 |    | 
  | 104 |  | 
  | 105 |  | 
  | 106 |  | 
  | 107 |  | 
  | 108 |  | 
  | 109 |   function enviaMensajeMqtt(mensaje, topico) { | 
  | 110 |    const mensajeMqtt = new Paho.MQTT.Message(mensaje) | 
  | 111 |    mensajeMqtt.destinationName = topico | 
  | 112 |    cliente.send(mensajeMqtt) | 
  | 113 |   } | 
  | 114 |  | 
  | 115 |  </script> | 
  | 116 |  | 
  | 117 | </body> | 
  | 118 |  | 
  | 119 | </html> |