|
2 | 2 | <html> |
3 | 3 | <head> |
4 | 4 | <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> |
| 5 | + <title>RabbitMQ Web MQTT Example</title> |
| 6 | + <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> |
5 | 7 | <script src="mqttws31.js" type="text/javascript"></script> |
6 | | - <script type="text/javascript"> |
7 | | - |
8 | | - //sample HTML/JS script that will publish/subscribe to topics in the Google Chrome Console |
9 | | - //by Matthew Bordignon @bordignon on twitter. |
10 | | - |
11 | | - var wsbroker = "localhost"; //mqtt websocket enabled broker |
12 | | - var wsport = 15675 // port for above |
13 | | - |
14 | | - var client = new Paho.MQTT.Client(wsbroker, wsport, "/ws", |
15 | | - "myclientid_" + parseInt(Math.random() * 100, 10)); |
16 | | - |
17 | | - client.onConnectionLost = function (responseObject) { |
18 | | - console.log("connection lost: " + responseObject.errorMessage); |
19 | | - }; |
20 | | - |
21 | | - client.onMessageArrived = function (message) { |
22 | | - console.log(message.destinationName, ' -- ', message.payloadString); |
23 | | - }; |
24 | | - |
25 | | - var options = { |
26 | | - timeout: 3, |
27 | | - onSuccess: function () { |
28 | | - console.log("mqtt connected"); |
29 | | - // Connection succeeded; subscribe to our topic, you can add multile lines of these |
30 | | - client.subscribe('/World', {qos: 1}); |
31 | | - |
32 | | - |
33 | | - //use the below if you want to publish to a topic on connect |
34 | | - message = new Paho.MQTT.Message("Hello"); |
35 | | - message.destinationName = "/World"; |
36 | | - client.send(message); |
37 | | - |
38 | | - }, |
39 | | - onFailure: function (message) { |
40 | | - console.log("Connection failed: " + message.errorMessage); |
| 8 | + <style> |
| 9 | + .box { |
| 10 | + width: 440px; |
| 11 | + float: left; |
| 12 | + margin: 0 20px 0 20px; |
41 | 13 | } |
42 | | - }; |
43 | 14 |
|
44 | | - function init() { |
45 | | - client.connect(options); |
46 | | - } |
| 15 | + .box div, .box input { |
| 16 | + border: 1px solid; |
| 17 | + -moz-border-radius: 4px; |
| 18 | + border-radius: 4px; |
| 19 | + width: 100%; |
| 20 | + padding: 5px; |
| 21 | + margin: 3px 0 10px 0; |
| 22 | + } |
47 | 23 |
|
48 | | - </script> |
| 24 | + .box div { |
| 25 | + border-color: grey; |
| 26 | + height: 300px; |
| 27 | + overflow: auto; |
| 28 | + } |
| 29 | + |
| 30 | + div code { |
| 31 | + display: block; |
| 32 | + } |
| 33 | + |
| 34 | + #first div code { |
| 35 | + -moz-border-radius: 2px; |
| 36 | + border-radius: 2px; |
| 37 | + border: 1px solid #eee; |
| 38 | + margin-bottom: 5px; |
| 39 | + } |
| 40 | + |
| 41 | + #second div { |
| 42 | + font-size: 0.8em; |
| 43 | + } |
| 44 | + </style> |
| 45 | + <link href="main.css" rel="stylesheet" type="text/css"/> |
49 | 46 | </head> |
50 | | - <body onload="init();"> |
51 | | - </body> |
| 47 | + <body lang="en"> |
| 48 | + <h1>RabbitMQ Web MQTT Example</h1> |
| 49 | + |
| 50 | + <div id="first" class="box"> |
| 51 | + <h2>Received</h2> |
| 52 | + <div></div> |
| 53 | + <form><input autocomplete="off" value="Type here..."></input></form> |
| 54 | + </div> |
| 55 | + |
| 56 | + <div id="second" class="box"> |
| 57 | + <h2>Logs</h2> |
| 58 | + <div></div> |
| 59 | + </div> |
| 60 | + |
| 61 | + <script> |
| 62 | + var has_had_focus = false; |
| 63 | + var pipe = function(el_name, send) { |
| 64 | + var div = $(el_name + ' div'); |
| 65 | + var inp = $(el_name + ' input'); |
| 66 | + var form = $(el_name + ' form'); |
52 | 67 |
|
| 68 | + var print = function(m, p) { |
| 69 | + p = (p === undefined) ? '' : JSON.stringify(p); |
| 70 | + div.append($("<code>").text(m + ' ' + p)); |
| 71 | + div.scrollTop(div.scrollTop() + 10000); |
| 72 | + }; |
| 73 | + |
| 74 | + if (send) { |
| 75 | + form.submit(function() { |
| 76 | + send(inp.val()); |
| 77 | + inp.val(''); |
| 78 | + return false; |
| 79 | + }); |
| 80 | + } |
| 81 | + return print; |
| 82 | + }; |
| 83 | + |
| 84 | + var print_first = pipe('#first', function(data) { |
| 85 | + message = new Paho.MQTT.Message(data); |
| 86 | + message.destinationName = "/topic/test"; |
| 87 | + debug("SEND ON " + message.destinationName + " PAYLOAD " + data); |
| 88 | + client.send(message); |
| 89 | + }); |
| 90 | + |
| 91 | + var debug = pipe('#second'); |
| 92 | + |
| 93 | + var wsbroker = "localhost"; //mqtt websocket enabled broker |
| 94 | + var wsport = 15675 // port for above |
| 95 | + |
| 96 | + var client = new Paho.MQTT.Client(wsbroker, wsport, "/ws", |
| 97 | + "myclientid_" + parseInt(Math.random() * 100, 10)); |
| 98 | + |
| 99 | + client.onConnectionLost = function (responseObject) { |
| 100 | + debug("CONNECTION LOST - " + responseObject.errorMessage); |
| 101 | + }; |
| 102 | + |
| 103 | + client.onMessageArrived = function (message) { |
| 104 | + debug("RECEIVE ON " + message.destinationName + " PAYLOAD " + message.payloadString); |
| 105 | + print_first(message.payloadString); |
| 106 | + }; |
| 107 | + |
| 108 | + var options = { |
| 109 | + timeout: 3, |
| 110 | + onSuccess: function () { |
| 111 | + debug("CONNECTION SUCCESS"); |
| 112 | + client.subscribe('/topic/test', {qos: 1}); |
| 113 | + }, |
| 114 | + onFailure: function (message) { |
| 115 | + debug("CONNECTION FAILURE - " + message.errorMessage); |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + debug("CONNECT TO " + wsbroker + ":" + wsport); |
| 120 | + client.connect(options); |
| 121 | + |
| 122 | + $('#first input').focus(function() { |
| 123 | + if (!has_had_focus) { |
| 124 | + has_had_focus = true; |
| 125 | + $(this).val(""); |
| 126 | + } |
| 127 | + }); |
| 128 | + </script> |
| 129 | + </body> |
53 | 130 | </html> |
0 commit comments