daily pastebin goal
44%
SHARE
TWEET

Untitled

a guest Sep 20th, 2016 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!doctype html>
  2. <html>
  3.   <head>
  4. <script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.min.js" type="text/javascript"></script>
  5. <script
  6.               src="https://code.jquery.com/jquery-2.2.4.min.js"
  7.               integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
  8.               crossorigin="anonymous"></script>
  9. <script>
  10. $(function() {
  11.     $("#send-msg").click(send);
  12.         // Create a client instance
  13.     client = new Paho.MQTT.Client("pcfeib425t.vsb.cz", Number(9999),"/ws", "mer0061");
  14.  
  15.     // set callback handlers
  16.     client.onConnectionLost = onConnectionLost;
  17.     client.onMessageArrived = onMessageArrived;
  18.  
  19.     let will = new Paho.MQTT.Message("Will msg from mer0061, cya");
  20.     will.destinationName = "/mschat/all/mer0061";
  21.     // connect the client
  22.     client.connect({
  23.         onSuccess:onConnect,
  24.         onFailure: onFail,
  25.         userName: "mobilni",
  26.         password: "Systemy",
  27.         willMessage: will
  28.  
  29.     });
  30.  
  31.  
  32.  
  33.     function send() {
  34.     console.log("sending");
  35.         let msg = new Paho.MQTT.Message(Math.floor(Date.now() / 1000) + " " + $("#msg").val());
  36.         $("#msg").val("");
  37.         msg.destinationName = "/mschat/all/mer0061";
  38.         client.send(msg);
  39.     }
  40.  
  41.     function onFail(ctx, code, msg) {
  42.         console.log("Failed to connect: " + msg);
  43.     }
  44.     // called when the client connects
  45.     function onConnect() {
  46.       // Once a connection has been made, make a subscription and send a message.
  47.       console.log("onConnect");
  48.       client.subscribe("/mschat/#");
  49.       //message.destinationName = "/mschat/all/mer0061";
  50.       //message = new Paho.MQTT.Message(Math.floor(Date.now() / 1000) + " -> Hello");
  51.       //client.send(message);
  52.     }
  53.  
  54.  
  55.  
  56.     // called when the client loses its connection
  57.     function onConnectionLost(responseObject) {
  58.       if (responseObject.errorCode !== 0) {
  59.         console.log("onConnectionLost:"+responseObject.errorMessage);
  60.       }
  61.     }
  62.  
  63.     // called when a message arrives
  64.     function onMessageArrived(message) {
  65.         $("#messages").append($(`<div>${message.payloadString}</div>`))
  66.         console.log("onMessageArrived:"+message.payloadString);
  67.     }
  68. });
  69. </script>
  70.   </head>
  71.   <body>
  72. <div id="messages">
  73. </div>
  74.     <input type="text" id="msg" />
  75.     <button id="send-msg">Send</button>
  76.   </body>
  77. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top