Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to config the tls options #45

Open
tigerjiang opened this issue Mar 7, 2023 · 2 comments
Open

How to config the tls options #45

tigerjiang opened this issue Mar 7, 2023 · 2 comments

Comments

@tigerjiang
Copy link

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}
}

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}
}
`

@tigerjiang
Copy link
Author

I got the error from console
GrillUtils connect err = Error: mqtt://xxxx.iot.us-west-1.amazonaws.com/mqtt:8883

@Symous
Copy link

Symous commented May 30, 2023

use tcp to replace mqtt, use ssl to replace mqtts,
new Mqtt.Client('ssl://domain.com:port')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants