Ubuntu Mosquitto 代理 websocket 不工作

Ubuntu Mosquitto broker websocket is not working

我是 IoT 和 MQTT 通信协议的新手。我正在尝试通过 Websockets 从我的 Vue Web 应用程序连接在 Amazon Ec2 上运行的代理。我已经开始使用 mosquitto:

root@ip-xxx-xx-xx-xx:~# mosquitto -c /etc/mosquitto/conf.d/default.conf
1618518468: mosquitto version 1.6.7 starting
1618518468: Config loaded from /etc/mosquitto/conf.d/default.conf.
1618518468: Opening ipv4 listen socket on port 1883.
1618518468: Opening ipv6 listen socket on port 1883.
1618518468: Opening websockets listen socket on port 9001.

/etc/mosquitto/conf.d/default.conf 文件包含:

listener 1883 
protocol mqtt 
allow_anonymous true 

listener 9001 
protocol websockets 
allow_anonymous true

我的测试js文件是:

var mqtt = require('mqtt');
var count =0;
var client  = mqtt.connect("mqtt://xx.xxx.xxx.xxx",{clientId:"mqttjs01"});
console.log("connected flag  " + client.connected);

//handle incoming messages
client.on('message',function(topic, message, packet){
    console.log("message is "+ message);
    console.log("topic is "+ topic);
});


client.on("connect",function(){ 
console.log("connected  "+ client.connected);

})
//handle errors
client.on("error",function(error){
console.log("Can't connect" + error);
process.exit(1)});
//publish
function publish(topic,msg,options){
console.log("publishing",msg);

if (client.connected == true){
    
client.publish(topic,msg,options);

}
count+=1;
if (count==2) //ens script
    clearTimeout(timer_id); //stop timer
    client.end();   
}

//////////////


var options={
retain:true,
qos:1};
var topic="acs";
var message="test message";
var topic_list=["topic2","topic3","topic4"];
var topic_o={"topic22":0,"topic33":1,"topic44":1};
console.log("subscribing to topics");
client.subscribe(topic,{qos:0}); //single topic
client.subscribe(topic_list,{qos:1}); //topic list
client.subscribe(topic_o); //object
var timer_id=setInterval(function(){publish(topic,message,options);},5000);
//notice this is printed even before we connect
console.log("end of script");

但是我收到了这个错误:

New client connected from 176.xxx.xxx.xx as mqttjs01 (p2, c1, k60).
1618518546: Socket error on client mqttjs01, disconnecting.

我已经安装了 libwebsockets,我尝试了各种 mosquitto 版本。当前版本是:1.6.7.

我的客户或经纪人有什么问题吗?我该如何解决这个问题?

publish() 函数的末尾,if 语句缺少大括号,因此它没有按照您的预期执行。

function publish(topic,msg,options){
console.log("publishing",msg);

if (client.connected == true){
    
client.publish(topic,msg,options);

}
count+=1;
if (count==2) //ens script
    clearTimeout(timer_id); //stop timer
    client.end();   
}

让我们修复缩进,以便我们看得更清楚。

function publish(topic,msg,options){
  console.log("publishing",msg);

  if (client.connected == true){  
    client.publish(topic,msg,options);
  }

  count+=1;
  if (count==2) //ens script
    clearTimeout(timer_id); //stop timer
  
  client.end();   
}

如您所见,每当调用 publish() 时,总会调用 client.end()。如果您只想发布两次,则需要将 2 条语句包装在大括号中(这不是 python 空格有意义的地方)

  if (count==2) { //ens script
    clearTimeout(timer_id); //stop timer
    client.end();  
  }

您真的应该适当地缩进所有代码,这将使阅读和发现此类错误变得更加容易。

此外,@JDAllen 提到您没有使用 WebSocket 连接,除非此代码在浏览器中为 运行,沙箱将强制它成为 WebSocket 连接,即使您指定 mqtt:// 作为 URL 中的架构,您必须包含端口号才能使其实际连接。例如

ws://xxx.xxx.xxx.xxx:9001