Node.js mqtt ssl 脚本
Node.js mqtt ssl script
我制作了一个脚本,将我的 linux CentOS VM 作为客户端连接到我的 mosquitto MQTT 代理。目前只是尝试使用 SSL 进行连接。发布和订阅经纪人需要用户名和密码。但是当我 运行 脚本使用:
node websitemqttclient.js
我刚收到下面的消息,光标停留在空白处 space 大约 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
不会 return true
。
将对 client.subscribe()
的调用添加到 on connect
事件处理程序,以便您实际上有一些消息来触发 on message
事件处理程序
我制作了一个脚本,将我的 linux CentOS VM 作为客户端连接到我的 mosquitto MQTT 代理。目前只是尝试使用 SSL 进行连接。发布和订阅经纪人需要用户名和密码。但是当我 运行 脚本使用:
node websitemqttclient.js
我刚收到下面的消息,光标停留在空白处 space 大约 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();
connect
事件处理程序完成之前,client.connected
不会 return true
。
将对 client.subscribe()
的调用添加到 on connect
事件处理程序,以便您实际上有一些消息来触发 on message
事件处理程序