async connect()

in content/AzIotHubClient.js [96:182]


  async connect () {
    let userName = `${this.host}/${this.deviceId}/?api-version=2020-05-31-preview`
    if (this.modelId) userName += `&model-id=${this.modelId}`
    const password = await generateSasToken(`${this.host}/devices/${this.deviceId}`, this.key, null, 60)
    return new Promise((resolve, reject) => {
      this.client.onConnectionLost = (err) => {
        console.log(err)
        this.connected = false
        this.disconnectCallback(err)
        reject(err)
      }

      const willMsg = new Paho.MQTT.Message('')
      willMsg.destinationName = 'willMessage'

      this.client.onMessageArrived = (/** @type {Paho.MQTT.Message} */ m) => {
        const destinationName = m.destinationName
        const payloadString = m.payloadString
        // console.log('On Msg Arrived to ' + destinationName)
        // console.log(payloadString)
        if (destinationName === '$iothub/twin/res/200/?$rid=' + this.rid) {
          this._onReadTwinCompleted(payloadString)
        }
        if (destinationName.startsWith('$iothub/twin/res/204/?$rid=' + this.rid)) {
          this._onUpdateTwinCompleted()
        }
        if (destinationName.indexOf('methods/POST') > 1) {
          const destParts = destinationName.split('/') // $iothub/methods/POST/myCommand/?$rid=2
          const methodName = destParts[3]
          const ridPart = destParts[4]
          const rid = parseInt(ridPart.split('=')[1])
          this.directMethodCallback(methodName, payloadString, rid)
        }
        if (destinationName.indexOf('twin/PATCH/properties/desired') > 1) {
          this.desiredPropCallback(payloadString)
        }
        if (destinationName.startsWith(DEVICE_C2D_COMMAND_TOPIC.slice(0, -1).replace('{deviceid}', this.deviceId))) {
          const url = decodeURIComponent(destinationName)
          const methodName = url.substring(url.indexOf("&method-name=")+13)
          this.c2dCallback(methodName, payloadString)
        }
      }
      this.client.connect({
        useSSL: true,
        userName: userName,
        timeout: 120,
        cleanSession: true,
        invocationContext: {},
        keepAliveInterval: 120,
        willMessage: willMsg,
        password: password,
        onSuccess: () => {
          this.connected = true
          console.log('Connected !!')
          this.client.subscribe(DEVICE_TWIN_RES_TOPIC, {
            qos: 0,
            invocationContext: {},
            onSuccess: () => { },
            onFailure: (err) => { throw err },
            timeout: 120
          })
          this.client.subscribe(DIRECT_METHOD_TOPIC, {
            qos: 0,
            invocationContext: {},
            onSuccess: () => { },
            onFailure: (err) => { throw err },
            timeout: 120
          })
          this.client.subscribe(DEVICE_TWIN_DESIRED_PROP_RES_TOPIC, {
            qos: 0,
            invocationContext: {},
            onSuccess: () => { },
            onFailure: (err) => { throw err },
            timeout: 120
          })
          this.client.subscribe(DEVICE_C2D_COMMAND_TOPIC.replace('{deviceid}', this.deviceId), {
            qos: 0,
            invocationContext: {},
            onSuccess: () => { },
            onFailure: (err) => { throw err },
            timeout: 120
          })
          resolve(this.deviceId)
        }
      })
    })
  }