Node.js MQTT SSL脚本 [英] Node.js mqtt ssl script

查看:35
本文介绍了Node.js MQTT SSL脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个脚本,将我的Linux CentOS VM作为客户端连接到MQTT代理。现在我正在尝试使用SSL进行连接。要发布和订阅代理,需要用户名和密码。但是当我使用以下命令运行脚本时:

node websitemqttclient.js

我刚刚收到下面的消息,在执行完成并可以输入新命令之前,光标在空白处停留了大约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();

推荐答案

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

将对client.subscribe()的调用添加到Onconnect事件处理程序,这样您实际上就有一些消息可以触发Onmessage事件处理程序

这篇关于Node.js MQTT SSL脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆