繁体   English   中英

Node.js mqtt ssl 脚本

[英]Node.js mqtt ssl script

我编写了一个脚本来将我的 linux CentOS VM 作为客户端连接到我的 mosquitto MQTT 代理。 目前只是尝试使用 SSL 进行连接。 要发布和订阅代理,需要用户名和密码。 但是当我使用以下命令运行脚本时:

node websitemqttclient.js

我刚刚收到下面的消息,cursor 在执行完成之前会在空白处放置大约 1.5 分钟,然后可以输入新命令。 我看到了折旧通知,但它似乎还不是问题。 此外,我已验证代理及其设置上的 8883 端口已打开,因此我认为问题出在客户端及其代码上。

user [bin]# node websitemqttclient.js
(node:30037) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permitted by RFC 6066. This will be ignored in a future version.
connected  false

我的脚本实际上是连接到我的代理还是只是坐在循环中而不连接到代理?

主要代码:

/////////////////////////////////////////////////////////////////////////////////////////
//setup
var mqtt    = require('mqtt'); //for client use
const fs = require('fs');
var count =0; //connected to an end script function
var caFile = fs.readFileSync("/pathway/bin/ca.crt");

var options={
host:'brokerip',
port:8883,
clientId:"yo",
username:"user",
password:"password",
protocol: 'mqtts',
clean:true,
rejectUnauthorized: false,
retain:false, 
ca:caFile 
}

var client  = mqtt.connect(options);
/////////////////////////////////////////////////////////////////////////////////////////
//connection dialog

//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)});
/////////////////////////////////////////////////////////////////////////////////////////



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

count+=1;  //quit script after the execution of two loops
if (count==2) //ens script
    clearTimeout(timer_id); //stop timer
    client.end();

在 on connect事件处理程序完成之前, client.connected不会返回true

将对client.subscribe()的调用添加到 on connect事件处理程序,以便您实际上有一些消息来触发 on message事件处理程序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM