对文档的解释很少,如果使用mqtt.js,我找不到任何使用节点js的例子。我需要使用tls将客户端连接到mqtt服务器。以下是我尝试建立连接的方法。
export interface SecurityOptions {
keyPath: string;
certPath: string;
cAPath: string;
passphrase: string;
rejectUnauthorized: boolean;
}
export interface MqttOptions {
clientId: string;
protocolId: string;
protocolVersion: number;
keepAlive: number;
clean: boolean;
reconnectPeriod: string;
will: IWillMessage;
}
let mqttOptions: MqttOptions;
let securityOptions: SecurityOptions;
mqttOptions = {
clientId: '100',
protocolId: 'MQTT',
protocolVersion: 4,
keepAlive: 1000,
clean: false,
reconnectPeriod: '1000',
will: willMessage
};
securityOptions = {
keyPath: 'G:/Projects/Kube2/edge-node-sdk-js/tls-key.pem',
certPath: 'G:/Projects/Kube2/edge-node-sdk-js/tls-cert.pem',
cAPath: 'G:/Projects/Kube2/edge-node-sdk-js/crt.ca.cg.pem',
passphrase: '',
rejectUnauthorized: true
};
this.client = mqtt.connect(url, tls.connect({
key: fs.readFileSync(securityOptions.keyPath),
ca: fs.readFileSync(securityOptions.cAPath),
cert: fs.readFileSync(securityOptions.certPath),
passphrase: securityOptions.passphrase
}),mqttOptions);
但它给我带来了以下错误。
Details:
code: EADDRNOTAVAIL
errno: EADDRNOTAVAIL
syscall: connect
address: 127.0.0.1
domainEmitter: [object Object]
domain: [object Object]
domainThrown: false
Stack:
Error: connect EADDRNOTAVAIL 127.0.0.1 - Local (0.0.0.0:5625)
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at connect (net.js:849:16)
at net.js:984:7
at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:64:16)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:83:10)
我在这里做错了什么?如何通过mqtt选项和tls选项连接到mqtt客户端?请指教。