我在raspberry pi上运行了一个基于paho的子程序,它使用用户名和密码监听cloudmqtt代理。 我将消息转发到蓝牙插座。
使用cordova app我发布消息“0”或“1”。它工作正常。但是当我在不同的手机上使用相同的应用程序时,只有最后打开的应用程序工作时才会出现问题。
简化事情......除了蓝牙之外的东西。
场景是这样的,我在cloudmqtt上有一个MQTT代理:
url:m12.cloudmqtt.com 港口:11395 用户名:user 密码:传递
当我使用此命令在raspberry pi上运行mosquitto_sub客户端时:
mosquitto_sub -h m12.cloudmqtt.com -p 11395 -u user -P pass -t sample / test
它运行然后我在两个不同的termianls中运行两个mosquitto_pub,命令如下:
mosquitto_pub -h m12.cloudmqtt.com -p 11395 -u user -P pass -t sample / test -m“Hi”
像魅力一样!
但是现在当我使用Paho MQTT插件从cordova应用程序使用这些MQTT凭证时,使用以下代码:
index.js
var connect = false;
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
onButton.addEventListener('touchend', app.sendOnSignal, false);
offButton.addEventListener('touchend', app.sendOffSignal, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
//alert("ondeviceready called");
app.connectFreeBroker();
},
// Update DOM on a Received Event
connectFreeBroker:function (id,s) {
//alert("connectFreeBroker called");
cordova.plugins.CordovaMqTTPlugin.connect({
url:"tcp://m12.cloudmqtt.com", //a public broker used for testing purposes only. Try using a self hosted broker for production.
port:11395,
connectionTimeout:3000,
keepAlive:6000,
isCleanSession:true,
clientid:"client-2",
username:"user",
password:"pass",
success:function(s){
//alert("connect success callback");
},
error:function(e){
//alert("connect error callback");
},
onConnectionLost:function (){
console.log("disconnect");
}
})
},
sendOnSignal:function (id,s) {
//alert("sendOnSignal called");
cordova.plugins.CordovaMqTTPlugin.publish({
topic:"sample/test",
payload:"1",
success:function(s){
//alert("send success callback");
document.getElementById("bulb_image").src = "images/bulb-glow.png";
},
error:function(e){
//alert("send error callback");
}
})
},
sendOffSignal:function (id,s) {
//alert("sendOffSignal called");
cordova.plugins.CordovaMqTTPlugin.publish({
topic:"sample/test",
payload:"0",
success:function(s){
//alert("send success callback");
document.getElementById("bulb_image").src = "images/bulb-unglow.png";
},
error:function(e){
//alert("send error callback");
}
})
}
};
app.initialize();
//代码结束
在第一个Android手机(Galaxy S5)上部署此应用程序时,它可以工作,而在raspberry pi上运行的子程序会收到该消息。现在,当在第二个Android手机(HTC欲望820)上部署具有不同客户端ID的相同应用程序时,它运行相同但在两部手机上同时运行相同的应用程序并且发送消息不起作用。只有来自最后打开应用程序的手机的消息才会发送消息。
我尝试使用不同的经纪人,但同样的问题。我尝试在4手机上使用该应用仍然没有运气。可能是我正在做的一个小错误,但会为我解决一个很大的问题,任何一种帮助赞赏! : - )
答案 0 :(得分:0)
Mqtt要求每个设备都有唯一的ID,因此请更改以下行:
clientid:"client-2",
类似于:
clientid:"client-" + parseInt(Math.random() * 100, 10)