Created
June 23, 2016 22:02
-
-
Save grahamoregan/8bae143047adb0575415025e27e9b893 to your computer and use it in GitHub Desktop.
Quick example of reading/writing for Kyle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript"></script> | |
<script> | |
var server = '...'; | |
var port = 8080; | |
var clientId = "unique-mqtt-client-id"; | |
// Create a client instance | |
client = new Paho.MQTT.Client(server, Number(port), clientId); | |
// set callback handlers | |
client.onConnectionLost = onConnectionLost; | |
client.onMessageArrived = onMessageArrived; | |
// connect the client | |
client.connect({onSuccess:onConnect, useSSL:true}); | |
var channel = "/test"; | |
// called when the client connects | |
function onConnect() { | |
var payload = { | |
"title": "json title", | |
"description": "a JSON object for testing" | |
}; | |
// Once a connection has been made, make a subscription and send a message. | |
console.log("onConnect"); | |
client.subscribe(channel); | |
message = new Paho.MQTT.Message(JSON.stringify(payload)); | |
message.destinationName = channel; | |
client.send(message); | |
} | |
// called when the client loses its connection | |
function onConnectionLost(responseObject) { | |
if (responseObject.errorCode !== 0) { | |
console.log("onConnectionLost:"+responseObject.errorMessage); | |
} | |
} | |
// called when a message arrives | |
function onMessageArrived(message) { | |
var payload = JSON.parse(message.payloadString); | |
console.log("title: "+ payload.title); | |
} | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment