Open tigerjiang opened 1 year ago
I want to connect aws iot core like as mqtt client. But i don't know how config the tlsoptions, could anyone meet the same issue?
the source code is bellow:
` // MQTT 客户端选项 const options = { clientId: 'mqtt-client-' + (Math.floor((Math.random() 100000) + 1)), reconnectPeriod: 30 1000, qos: 2, enableSsl: true, tls:{ caDer: this.fetchCaDer(), cert: this.fetchCert(), key: this.fetchPrivateKey(),
} }; const client = new Mqtt.Client("mqtt://xxx.iot.us-west-1.amazonaws.com:8883"); client.connect(options, err => { console.log("GrillUtils connect err = " + err.toString()) console.log("GrillUtils connect err = " + JSON.stringify(err)) }); client.on(Mqtt.Event.Message, (topic, message) => { console.log('Mqtt Message:', topic, message.toString()); console.log("GrillUtils message topic = " + topic + " data = " + data) this.subscribeState = true this.connected = true; this.selfCheck(CheckStatus.FOM_HEART_BEAT); if (data.value.hasOwnProperty("result")) { this.convertResultTopic(data); } else if (data.value.hasOwnProperty("heat")) { this.convertHeartbeatTopic(data); } else { //don't care other heartbeat data. this.convertSubscribeTopic(data) } }); client.on(Mqtt.Event.Connect, () => { console.log('MQTT Connect'); console.log("GrillUtils connect ") client.subscribe('heartbeat/' + this.grillID, (err) => { if (err) { console.log('Error subscribing to topic:', err); } else { console.log('Subscribed to topic'); } }); // 订阅主题 client.subscribe('result/' + this.grillID) client.subscribe('command/' + this.grillID) }); client.on(Mqtt.Event.Error, (error: string) => { console.log('MQTT Error:', error); }); client.on(Mqtt.Event.Disconnect, (cause: string) => { console.log('MQTT Disconnect:', cause); });
///tlsoptions fetchCaDer(){ const startLabel = "-----BEGIN CERTIFICATE-----"; const endLabel ="-----END CERTIFICATE-----" return ${startLabel} MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF ... ${endLabel} }
${startLabel} MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF ... ${endLabel}
fetchCert(){ const startLabel = "-----BEGIN CERTIFICATE-----"; const endLabel ="-----END CERTIFICATE-----" return ${startLabel} MIIDWjCCAkKgAwIBAgIVAKYgDNHgFHvnaJJhb9d0DmqOd0KwMA0GCSqGSIb3DQEB ... ${endLabel}
${startLabel} MIIDWjCCAkKgAwIBAgIVAKYgDNHgFHvnaJJhb9d0DmqOd0KwMA0GCSqGSIb3DQEB ... ${endLabel}
}
fetchPrivateKey(){ const startLabel = "-----BEGIN RSA PRIVATE KEY-----"; const endLabel ="-----END RSA PRIVATE KEY-----" return ${startLabel} MIIEowIBAAKCAQEAtTA7FQ2Yopw3aQ3w0EBzQj0nmERQGW5FYRTsSlEBBijx9/ge ... Ew18DGPwHBLDhC9FrlHAcXsLOrESj6V+WCshz45nLbe2E+3T9MB0 ${endLabel} } `
${startLabel} MIIEowIBAAKCAQEAtTA7FQ2Yopw3aQ3w0EBzQj0nmERQGW5FYRTsSlEBBijx9/ge ... Ew18DGPwHBLDhC9FrlHAcXsLOrESj6V+WCshz45nLbe2E+3T9MB0 ${endLabel}
I got the error from console GrillUtils connect err = Error: mqtt://xxxx.iot.us-west-1.amazonaws.com/mqtt:8883
GrillUtils connect err = Error: mqtt://xxxx.iot.us-west-1.amazonaws.com/mqtt:8883
use tcp to replace mqtt, use ssl to replace mqtts, new Mqtt.Client('ssl://domain.com:port')
tcp
mqtt
ssl
mqtts
new Mqtt.Client('ssl://domain.com:port')
I want to connect aws iot core like as mqtt client. But i don't know how config the tlsoptions, could anyone meet the same issue?
the source code is bellow:
` // MQTT 客户端选项 const options = { clientId: 'mqtt-client-' + (Math.floor((Math.random() 100000) + 1)), reconnectPeriod: 30 1000, qos: 2, enableSsl: true, tls:{ caDer: this.fetchCaDer(), cert: this.fetchCert(), key: this.fetchPrivateKey(),
///tlsoptions fetchCaDer(){ const startLabel = "-----BEGIN CERTIFICATE-----"; const endLabel ="-----END CERTIFICATE-----" return
${startLabel} MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF ... ${endLabel}
}fetchCert(){ const startLabel = "-----BEGIN CERTIFICATE-----"; const endLabel ="-----END CERTIFICATE-----" return
${startLabel} MIIDWjCCAkKgAwIBAgIVAKYgDNHgFHvnaJJhb9d0DmqOd0KwMA0GCSqGSIb3DQEB ... ${endLabel}
}
fetchPrivateKey(){ const startLabel = "-----BEGIN RSA PRIVATE KEY-----"; const endLabel ="-----END RSA PRIVATE KEY-----" return
${startLabel} MIIEowIBAAKCAQEAtTA7FQ2Yopw3aQ3w0EBzQj0nmERQGW5FYRTsSlEBBijx9/ge ... Ew18DGPwHBLDhC9FrlHAcXsLOrESj6V+WCshz45nLbe2E+3T9MB0 ${endLabel}
} `