使用javascript应用程序在MQTT服务器上发送一次
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用javascript应用程序在MQTT服务器上发送一次相关的知识,希望对你有一定的参考价值。
我有一个连接到MQTT代理服务器上的主题的应用程序,我使用我的javascript代码连接到Web套接字端口。我的问题,当我把我的应用程序放在网络服务器上,我一直保持连接和发送消息..永远不要停止。怎么能解决我的代码只连接一次,永远不再连接,除非我断开连接。这是我的代码。
client = new Paho.MQTT.Client("m23.cloudmqtt.com", 38788,"clientId-O8R8jUxaRM");
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
var options = {
useSSL: true,
userName: "peelmimp",
password: "iold2UAoIs8T",
onSuccess:onConnect,
onFailure:doFail
}
client.connect(options);
// 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("sensor8");
message = new Paho.MQTT.Message(" CloudMQTT");
message.destinationName = "sensor8";
client.send(message);
}
function doFail(e){
console.log(e);
}
// called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
client.connect(options);
}
}
// called when a message arrives
function onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);
var x = message.payloadString;
if(x == 1){ //park spot number 1
document.getElementById("park1").style.backgroundColor = "orange";
document.getElementById("park1p").style.fontSize="40px";
document.getElementById("park1p").innerhtml="A car is going to park at spot number 1";
}
if(x == 2){ //car parked at park1
document.getElementById("park1").style.backgroundColor = "red";
document.getElementById("park1p").innerHTML="spot number 1 is not available";
}
if(x == 3){ //car left park 1 and it is available
document.getElementById("park1").style.backgroundColor = "green";
document.getElementById("park1p").innerHTML="spot number 1 is available";
}
if(x == 4){ //park spot number 2
document.getElementById("park2").style.backgroundColor = "orange";
document.getElementById("park2p").style.fontSize="40px";
document.getElementById("park2p").innerHTML="A car is going to park at spot number 2";
}
if(x == 5){ //car parked at park2
document.getElementById("park2").style.backgroundColor = "red";
document.getElementById("park2p").innerHTML="spot number 2 is not available";
}
if(x == 6){ //car left park 2 and it is available
document.getElementById("park2").style.backgroundColor = "green";
document.getElementById("park2p").innerHTML="spot number 2 is available";
}
if(x == 7){ //park spot number 3
document.getElementById("park3").style.backgroundColor = "orange";
document.getElementById("park3p").style.fontSize="40px";
document.getElementById("park3p").innerHTML="A car is going to park at spot number 3";
}
if(x == 8){ //car parked at park3
document.getElementById("park3").style.backgroundColor = "red";
document.getElementById("park3p").innerHTML="spot number 3 is not available";
}
if(x == 9){ //car left park 3 and it is available
document.getElementById("park3").style.backgroundColor = "green";
document.getElementById("park3p").innerHTML="spot number 3 is available";
}
if(x == 10){ //park spot number 4
document.getElementById("park4").style.backgroundColor = "orange";
document.getElementById("park4p").style.fontSize="40px";
document.getElementById("park4p").innerHTML="A car is going to park at spot number 4";
}
if(x == 11){ //car parked at park4
document.getElementById("park4").style.backgroundColor = "red";
document.getElementById("park4p").innerHTML="spot number 4 is not available";
}
if(x == 12){ //car left park 4 and it is available
document.getElementById("park4").style.backgroundColor = "green";
document.getElementById("park4p").innerHTML="spot number 4 is available";
}
}
答案
您有一个固定的客户端ID,并且代码在断开连接时会自动重新连接。
在您的on('connected')
回调中,您发布了一条消息。
由于代理只允许单个客户端与任何给定的客户端ID连接,因此当新的客户端连接时,它将始终关闭最旧的客户端。一旦多个浏览器打开页面,这将触发重新连接循环。因此,触发连接上发布的消息将被多次发布。
以上是关于使用javascript应用程序在MQTT服务器上发送一次的主要内容,如果未能解决你的问题,请参考以下文章