Created
September 14, 2018 02:10
-
-
Save KEVINALBO13/2e33bfe334428e63d9c663a7e20f3e7f to your computer and use it in GitHub Desktop.
MQTT.JS
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
// Create a client instance | |
var client = new Paho.MQTT.Client('test.mosquitto.org', 1883, "clientId"); | |
// set callback handlers | |
client.onConnectionLost = onConnectionLost; | |
client.onMessageArrived = onMessageArrived; | |
// connect the client | |
client.connect({onSuccess:onConnect}); | |
// called when the client connects | |
function onConnect() { | |
// Once a connection has been made, make a subscription and send a message. | |
console.log("onConnect"); | |
client.subscribe("World"); | |
message = new Paho.MQTT.Message("Hello"); | |
message.destinationName = "World"; | |
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) { | |
console.log("onMessageArrived:"+message.payloadString); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment