Skip to content

Commit

Permalink
cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan-Piet Mens committed Jul 3, 2014
1 parent 99e7db4 commit 259f36e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 45 deletions.
15 changes: 8 additions & 7 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
websocketserver = '127.0.0.1';
websocketport = 9001;
topic = '#';
host = '127.0.0.1'; // hostname or IP address
port = 9001;
topic = '#'; // topic to subscribe to
useTLS = false;
// username = null;
// password = null;
username = "jjolie";
password = "aa";
username = null;
password = null;
// username = "jjolie";
// password = "aa";
cleansession = true;
82 changes: 44 additions & 38 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,72 @@
<script src="config.js" type="text/javascript"></script>

<script type="text/javascript">
var client = new Messaging.Client(
websocketserver,
websocketport,
var mqtt;
var reconnectTimeout = 2000;

function MQTTconnect() {
mqtt = new Messaging.Client(
host,
port,
"web_" + parseInt(Math.random() * 100,
10));
var options = {
timeout: 3,
useSSL: useTLS,
cleanSession: cleansession,
onSuccess: onConnect,
onFailure: function (message) {
$('#status').val("Connection failed: " + message.errorMessage + "Retrying");
setTimeout(MQTTconnect, reconnectTimeout);
}
};

client.onConnectionLost = function (responseObject) {
$('#status').val("connection lost: " + responseObject.errorMessage);
mqtt.onConnectionLost = onConnectionLost;
mqtt.onMessageArrived = onMessageArrived;

};
if (username != null) {
options.userName = username;
options.password = password;
}
console.log("Host="+ host + ", port=" + port + " TLS = " + useTLS + " username=" + username + " password=" + password);
mqtt.connect(options);
}

client.onMessageArrived = function (message) {
var topic = message.destinationName;
var payload = message.payloadString;
function onConnect() {
$('#status').val('Connected to ' + host + ':' + port);
// Connection succeeded; subscribe to our topic
mqtt.subscribe(topic, {qos: 0});
$('#topic').val(topic);
}

function onConnectionLost(response) {
setTimeout(MQTTconnect, reconnectTimeout);
$('#status').val("connection lost: " + responseObject.errorMessage + ". Reconnecting");

$('#ws').prepend('<li>' + topic + ' = ' + payload + '</li>');
};

var options = {
timeout: 3,
useSSL: useTLS,
onSuccess: function () {
$('#status').val('Connected to ' + websocketserver + ':' + websocketport);
// Connection succeeded; subscribe to our topic
client.subscribe(topic, {qos: 0});
$('#topic').val(topic);
function onMessageArrived(message) {

},
onFailure: function (message) {
$('#status').val("Connection failed: " + message.errorMessage);
var topic = message.destinationName;
var payload = message.payloadString;

}
$('#ws').prepend('<li>' + topic + ' = ' + payload + '</li>');
};

function init() {
/* Connect to MQTT broker */
if (username != null) {
options.userName = username;
options.password = password;
}
console.log("TLS = " + useTLS + " username=" + username + " password=" + password);
client.connect(options);
}

$(document).ready(function() {
init();
MQTTconnect();
});

</script>
</head>
<body>
<h1>Mosquitto Websockets</h1>
<div>
Subscribed to <input type='text' id='topic' disabled /> Status: <input type='text' id='status' size="80" disabled />
<h3>Messages</h3>
<div>Subscribed to <input type='text' id='topic' disabled />
Status: <input type='text' id='status' size="80" disabled /></div>

<ul id='ws' style="font-family: 'Courier New', Courier, monospace;">
</ul>
<ul id='ws' style="font-family: 'Courier New', Courier, monospace;"></ul>
</div>

</div>
</body>
</html>

0 comments on commit 259f36e

Please sign in to comment.