mqttws31.js下载:https://pan.baidu.com/s/1-8svGSs78bIUfHSnRRiaqA
提取码:utla
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/mqttws31.js"></script>
</head>
<body>
<script>
var wsbroker = "10.0.0.30"; // mqtt websocket enabled broker ip
var wsport = 61623; // port for above
var client = new Paho.MQTT.Client(wsbroker, wsport, "/ws",
"myclientid_" + parseInt(Math.random() * 100, 10));
/* client.userName("admin");
client.password("password"); */
client.onConnectionLost = function (responseObject) {
console.log("CONNECTION LOST - " + responseObject.errorMessage);
};
client.onMessageArrived = function (message) {
console.log("RECEIVE ON " + message.destinationName + " PAYLOAD " + message.payloadString);
alert(message.payloadString);
print_first(message.payloadString);
};
var sessionId = "mtest";
var options = {
userName:"admin",
password:"password",
timeout: 3,
keepAliveInterval: 30,
onSuccess: function () {
//console.log(("CONNECTION SUCCESS");
// 这样就可以做到点对点通信
client.subscribe(sessionId, {qos: 1});
//alert(message.payloadMessage);
},
onFailure: function (message) {
console.log("CONNECTION FAILURE - " + message.errorMessage);
}
};
if (location.protocol == "https:") {
options.useSSL = true;
}
console.log("CONNECT TO " + wsbroker + ":" + wsport);
client.connect(options);
</script>
</body>
</html>