// 创建一个jsMQTT客户端实例,clientId是唯一标识符,地址和端口号为服务的地址和端口
client = new Paho.MQTT.Client(location.hostname, Number(location.port), "clientId")
// 客户端连接丢失的处理
client.onConnectionLost = onConnectionLost
client.onMessageArrived = onMessageArrived
client.connect({onSuccess:onConnect});
function onConnect() {
console.log("onConnect");
client.subscribe("World");
message = new Paho.MQTT.Message("Hello");
message.destinationName = "World";
client.send(message);
}
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
function onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);
}